我有 GridView 4x25 ,其中包含 RadioButton&每个单元格中的TextView 。 TextView采用单元格索引,因此我的网格如下所示:
o1 o2 o3 o4
o5 o6 o7 o8
...
o97 o98 o99 o100
我的适配器:
public class FieldAdapter extends BaseAdapter {
Context context; TextView Cell_num; int index = 0;
public FieldAdapter(Context context)
{
this.context=context;
}
@Override
public long getItemId(int position) {
return position;
}
public Object getItem(int position) {
return 0;
}
public int getCount()
{
return 100;
}
public View getView(int pos, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.cell, parent, false);
index++;
}
Log.d("Cell","Created");
Cell_num = (TextView)convertView.findViewById(R.id.top_cell_text);
Cell_num.setText(Integer.toString(index));
return convertView;
}
}
它的效果很好,我的 LogCat 中的每个单元格都有 100 条消息。
Log.d("细胞""创建&#34);
但,如果我向下滚动GridView ,此消息将继续显示 - 4个单元格隐藏在屏幕上边缘后面4个单元格。
如果我向后滚动GridView并查看隐藏的那些单元格,则所有TextView都会使用值" 28" ,而不是单元格索引。所以,滚动后 ,我看到这张图片:
o28 o28 o28 o28
o28 o28 o28 o28
...
o28 o28 o28 o28
此外,如果我在滚动之前检查一些RadioButton,它会在每次从屏幕上边缘隐藏/重新播放后改变它的位置。
在我看来,它是某种神奇的=(
需要帮助。
问题仍然存在。我改变了我的适配器:
public class FieldAdapter extends BaseAdapter {
Context context; TextView Cell_num;
LayoutInflater inflater;
public FieldAdapter(Context context)
{
this.context=context;
inflater = ((Activity) context).getLayoutInflater();
}
// id по позиции
@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)
{
convertView = inflater.inflate(R.layout.cell, parent, false);
Cell_num = (TextView)convertView.findViewById(R.id.top_cell_text);
Cell_num.setText(Integer.toString(pos));
Log.d("Filled cell number",Integer.toString(pos));
}
return convertView;
}
}
我的LogCat(带有我的评论):
05-07 11:44:32.151: D/States(15888): Button pressed
05-07 11:44:33.603: D/States(15888): Button pressed
05-07 11:44:33.603: D/States(15888): Activity2: onPause()
/* Start filling cells*/
05-07 11:44:33.653: D/Filled cell number(15888): 0
05-07 11:44:33.673: I/Adreno200-EGLSUB(15888): <ConfigWindowMatch:2087>: Format RGBA_8888.
05-07 11:44:33.683: D/Filled cell number(15888): 1
05-07 11:44:33.683: D/Filled cell number(15888): 2
05-07 11:44:33.693: D/Filled cell number(15888): 3
05-07 11:44:33.693: D/Filled cell number(15888): 4
05-07 11:44:33.703: D/Filled cell number(15888): 5
05-07 11:44:33.703: D/Filled cell number(15888): 6
05-07 11:44:33.703: D/Filled cell number(15888): 7
05-07 11:44:33.713: D/Filled cell number(15888): 8
05-07 11:44:33.713: D/Filled cell number(15888): 9
05-07 11:44:33.713: D/Filled cell number(15888): 10
05-07 11:44:33.723: D/Filled cell number(15888): 11
05-07 11:44:33.723: D/Filled cell number(15888): 12
05-07 11:44:33.733: D/Filled cell number(15888): 13
05-07 11:44:33.733: D/Filled cell number(15888): 14
05-07 11:44:33.743: D/Filled cell number(15888): 15
05-07 11:44:33.743: D/Filled cell number(15888): 16
05-07 11:44:33.743: D/Filled cell number(15888): 17
05-07 11:44:33.753: D/Filled cell number(15888): 18
05-07 11:44:33.753: D/Filled cell number(15888): 19
05-07 11:44:33.763: D/Filled cell number(15888): 20
05-07 11:44:33.763: D/Filled cell number(15888): 21
05-07 11:44:33.763: D/Filled cell number(15888): 22
05-07 11:44:33.773: D/Filled cell number(15888): 23
/*Stops filling cells*/
05-07 11:44:33.783: D/Filled cell number(15888): 0 /*Starts again? Why only 1 cell with 0 index?*/
05-07 11:44:34.033: D/States(15888): Activity2: onStop()
05-07 11:44:34.033: D/States(15888): Activity2: onDestroy()
/* AFTER SCROLLING*/
05-07 11:44:55.376: D/Filled cell number(15888): 25
05-07 11:44:55.376: D/Filled cell number(15888): 26
05-07 11:44:55.386: D/Filled cell number(15888): 27 /*Why only 25 26 27 ? */
只有27个单元格在圆圈中创建,随机改变其在网格上的位置。
答案 0 :(得分:0)
这不适合适配器的工作方式。任何时候进入视图时都会调用getView。这就是getView具有int position
参数的原因。使用它而不是index++
。
Cell_num.setText(Integer.toString(pos));
答案 1 :(得分:0)
我认为问题是因为每次开始创建行视图时都会声明布局膨胀。因此,最好在构造函数本身中声明布局膨胀。 喜欢这个
LayoutInflater inflater;
public FieldAdapter(Context context)
{
this.context=context;
inflater = ((Activity) context).getLayoutInflater();
}
因此,每次在滚动视图时数据发生变化时都会声明视图Inflate,并且
user1199931表示你不想按索引++添加你的索引,因为 在获取视图方法中,您将获得单元格的行位置。输入 改变后的代码就像这样
,
public class FieldAdapter extends BaseAdapter {
Context context; TextView Cell_num; int index = 0;
LayoutInflater inflater;
public FieldAdapter(Context context)
{
this.context=context;
inflater = ((Activity) context).getLayoutInflater();
}
@Override
public long getItemId(int position) {
return position;
}
public Object getItem(int position) {
return 0;
}
public int getCount()
{
return 100;
}
public View getView(int pos, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = inflater.inflate(R.layout.cell, parent, false);
// index++; as user1199931 said your need not want an increment section here
}
Log.d("Cell","Created");
Cell_num = (TextView)convertView.findViewById(R.id.top_cell_text);
Cell_num.setText(Integer.toString(pos));
return convertView;
}
}
希望这会对你有所帮助,谢谢你