2012-12-04 21 views
1

我已經閱讀了很多關於GridViews和ViewHolders,但沒有任何東西可以幫助我。不知道它是否有可能,或者我不能管理它。 問題:GridView表示一個Sudoku字段,如果用戶輸入一個數字或另一個GridView來顯示可能數字的鉛筆標記,則該字段可以包含TextView。 想法:一個ViewHolder,它包含一個GridView和一個TextView。如果TextView的文本是空的,則應該顯示鉛筆標記(在GridView中),否則應該看到帶有編號的TextView。 繼承人我的適配器代碼: (也許有一些問題與佈局其完全不直觀-.-?)通過ViewHolder在GridView中更改視圖可能嗎?

編輯:細胞是視圖,它定義了在各個領域開始意見的數組。所以如果在字段x中已經是數字y,則單元格[x]包含文本集y的文本視圖。 (我猜它有點難看,但即時通訊不熟悉GridView的,有很多的問題:()

public class ViewAdapter extends BaseAdapter{ 
Context context; 
View[] cells; 
int size; 

public ViewAdapter(Context context, View[] cells, int size) { 
    this.context = context; 
    this.cells = cells; 
    this.size = size; 
} 

public int getCount() { 
    return 81; 
} 

public Object getItem(int position) { 
    return null; 
} 

public long getItemId(int position) { 
    return 0; 
} 

public View getView(int position, View target, ViewGroup parent) { 
    ViewHolder holder; 
    if (target == null) { 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     target = inflater.inflate(R.layout.activity_game, parent, false); 
     holder = new ViewHolder(); 
     if (cells[position] instanceof GridView) { 
      holder.gv = (GridView)cells[position]; 
      holder.tv = new TextView(context); 
      holder.tv.setText(""); 
     } else if (cells[position] instanceof TextView){ 
      holder.gv = new GridView(context); 
      holder.gv.setNumColumns(3); 
      holder.gv.setClickable(false); 
      holder.tv = (TextView)cells[position]; 
     } 
     target.setTag(holder); 
    } else { 
     holder = (ViewHolder)target.getTag(); 
    } 

    GridView gv = holder.gv; 
    gv.setBackgroundColor(Color.WHITE); 
    gv.setLayoutParams(new GridView.LayoutParams(20, 20));  

    TextView tv = holder.tv; 
    tv.setGravity(Gravity.CENTER); 
    tv.setLayoutParams(new GridView.LayoutParams(LayoutParams.MATCH_PARENT, size));  
    tv.setBackgroundColor(Color.WHITE); 
    tv.setTextAppearance(context, android.R.style.TextAppearance_Large); 

    return target; 
} 

class ViewHolder { 
    GridView gv; 
    TextView tv; 
} 
} 

電賀月 並感謝任何答案

回答

0

好吧,我找到了解決辦法我自己。我只使用TextView並在3行文本中做鉛筆標記這是我發現的最簡單的方法 但是,我仍然想知道是否可以在Views之間切換

相關問題