我正在使用GridView。基本上,我确保getCount()返回9,但是当我调试代码时,我看到getView()只被调用了3次。我不明白为什么会这样,因为我清楚地告诉它要返回9次。
public class Level1BoardAdapter extends BaseAdapter {
private Context context;
private int level;
public Level1BoardAdapter(Context context, int level) {
this.context = context;
this.level = level;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 9;
}
@Override
public View getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Button button = new Button(context);
button.setOnClickListener(new ButtonPressed(context, level));
com.example.titanictictactoe.Board.keys.put(position, button);
return button;
}
}
这在以下类中调用:
public class Level2BoardAdapter extends BaseAdapter {
private Context context;
public Level2BoardAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 9;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
GridView board = new GridView(context);
board.setNumColumns(3);
board.setBackgroundColor(Color.BLACK);
board.setAdapter(new Level1BoardAdapter(context, 2));
Log.d("Count", String.format("%1d", board.getCount()));
return board;
}
}
答案 0 :(得分:0)
getView()
仅在屏幕上显示(或准备显示)时调用。由于屏幕尺寸有限,它不一定显示所有项目。在您的情况下,您的屏幕可以大致显示2到3个项目视图。
答案 1 :(得分:0)
滚动时,列表视图会发生更多的getView()调用。
此外,我认为您需要重新使用行视图,而不是在每个滚动上重新创建它们。修改getView()
以有效使用convertView
参数。