我有4x25 GridView。每个单元包含RadioButton& TextView的。
我的适配器:
public class FieldAdapter extends BaseAdapter {
Context context; TextView Cell_num; View Cell;
LayoutInflater inflater;
public FieldAdapter(Context context)
{
this.context=context;
inflater = ((Activity) context).getLayoutInflater();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return 0;
}
public int getCount()
{
return 100;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent)
{
if (convertView == null)
{
Cell = new View (context);
Cell = inflater.inflate(R.layout.cell, parent, false);
Cell_num = (TextView)Cell.findViewById(R.id.top_cell_text);
Cell_num.setText(Integer.toString(pos));
Log.d("Filled cell number",Integer.toString(pos));
}
else {
Log.d("Else","View already exist");
Cell = (View) convertView;
}
return Cell;
}
}
我的活动类:
public class FieldActivity extends Activity {
GridView Field; FieldAdapter FieldAdapter; MyMethods Method = new MyMethods();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.field);
Field = (GridView) findViewById(R.id.Field);
FieldAdapter = new FieldAdapter(this);
Field.setAdapter(FieldAdapter);
Method.CreateGrid (Field, this);//here i set numColumns & strechmode
}//onCreate
}
创建网格后,我在屏幕上看到6行,共4列。它是24个元素(0-23)
我在LogCat中看到了什么(我在评论中添加了一些问题):
05-08 19:07:57.584: D/Filled cell number(19267): 0
05-08 19:07:57.594: I/Adreno200-EGLSUB(19267): <ConfigWindowMatch:2087>: Format RGBA_8888.
05-08 19:07:57.604: D/Else(19267): View already exist /*why else condition was triggered?*/
05-08 19:07:57.614: D/Filled cell number(19267): 1
05-08 19:07:57.614: D/Filled cell number(19267): 2
05-08 19:07:57.624: D/Filled cell number(19267): 3
05-08 19:07:57.624: D/Filled cell number(19267): 4
05-08 19:07:57.634: D/Filled cell number(19267): 5
05-08 19:07:57.634: D/Filled cell number(19267): 6
05-08 19:07:57.644: D/Filled cell number(19267): 7
05-08 19:07:57.644: D/Filled cell number(19267): 8
05-08 19:07:57.644: D/Filled cell number(19267): 9
05-08 19:07:57.654: D/Filled cell number(19267): 10
05-08 19:07:57.664: D/Filled cell number(19267): 11
05-08 19:07:57.674: D/Filled cell number(19267): 12
05-08 19:07:57.684: D/Filled cell number(19267): 13
05-08 19:07:57.684: D/Filled cell number(19267): 14
05-08 19:07:57.694: D/Filled cell number(19267): 15
05-08 19:07:57.694: D/Filled cell number(19267): 16
05-08 19:07:57.704: D/Filled cell number(19267): 17
05-08 19:07:57.704: D/Filled cell number(19267): 18
05-08 19:07:57.704: D/Filled cell number(19267): 19
05-08 19:07:57.714: D/Filled cell number(19267): 20
05-08 19:07:57.714: D/Filled cell number(19267): 21
05-08 19:07:57.724: D/Filled cell number(19267): 22
05-08 19:07:57.724: D/Filled cell number(19267): 23
05-08 19:07:57.734: D/Filled cell number(19267): 0 /*Why again 0 number? Why not 24?*/
然后,如果我尝试向下滚动:
05-08 19:13:15.517: D/Else(19985): View already exist
05-08 19:13:15.527: D/Filled cell number(19985): 25
05-08 19:13:15.537: D/Filled cell number(19985): 26
05-08 19:13:15.537: D/Filled cell number(19985): 27
05-08 19:13:15.587: D/Else(19985): View already exist
05-08 19:13:15.587: D/Else(19985): View already exist
05-08 19:13:15.587: D/Else(19985): View already exist
05-08 19:13:15.587: D/Else(19985): View already exist
05-08 19:13:15.827: D/Else(19985): View already exist
05-08 19:13:15.827: D/Else(19985): View already exist
... etc
为什么只创建25 26 27个细胞?为什么单元格停止创建并开始重复(否则条件)
如果我尝试回滚我的GridView - 我看到相同的图片。我有这样的混蛋:
3 2 1 0
7 6 5 4
8 9 10 11
...
需要帮助。
答案 0 :(得分:0)
以下是此问题的解决方案。
适配器:
ArrayList <View> Cells = new ArrayList <View>();
...
...
public void FillArray(ViewGroup parent) {
while (cellIndex < getCount()){
Cells.add(inflater.inflate(R.layout.cell,parent, false));
Log.d("ArrayList" + Integer.toString(cellIndex), "is null: " + (Cells.get(cellIndex) == null));
cellIndex++;
}
}
public View getView(int pos, View convertView, ViewGroup parent)
{
FillArray(parent);
Cell_num = (TextView)Cells.get(pos).findViewById(R.id.top_cell_text);
Cell_num.setText(Integer.toString(pos));
Log.d("Filled cell number",Integer.toString(pos));
Log.d("ArrayList" + Integer.toString(pos), "is null: " + (Cells.get(pos) == null));
return Cells.get(pos);
}
我使用ArrayList作为getView方法的View元素的源。即使滚动屏幕,它也会使每个单元格重新处于其状态。
然而,它使你的适配器在从屏幕边缘出现时重新填充单元格。