我这里有一个非常简单的适配器。我想在列表中有很多空格的地方。假设是 1,000。适配器后面的数据集只有 5 个元素。当这个适配器被加载到一个列表视图中时,一切看起来都很好,直到你滚出屏幕。然后位置重复并加载数据集中的数据。为什么?getCount() 在被覆盖时是否需要始终返回数据集中实际存在的大小?
private abstract class BlankEntriesAdapter extends BaseAdapter {
private final List<String> rowList;
public BlankEntriesAdapter(List<String> rowList) {
this.rowList = rowList;
}
@Override
public int getCount() {
//force a few blank spaces
return 1000;
}
@Override
public String getItem(int position) {
if (position < rowList.size()) {
return rowList.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
RowItem item = getItem(position);
if (item != null) {
return item.rowId;
}
return 0L;
}
@Override
public boolean isEnabled(int position) {
return position < rowList.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//ISSUE IS HERE. Position numbers start to repeat. Expect position to match what row was drawn to screen.
String stringItem = getItem(position);
/*Load string to view if not null. Code not shown.*/
}
}