嘿,我使用gridView来显示我的项目。一切都还可以,但在六件物品或滚动后,一些物品会被重复。
所以我觉得如果convertItem不为null,我还有事可做。但我如何加载下一个项目
一些简短的代码:
public CustomGridAdapter(Context context, ArrayList<Event> finalResult, double latitude, double longitude) {
this.posLatitude = latitude;
this.posLongitude = longitude;
this.context = context;
this.gridValues = finalResult;
this.app = new Application(context);
}
@Override
public int getCount() {
return gridValues.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// LayoutInflator to call external grid_item.xml file
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(R.layout.grid_item, null);
event = gridValues.get(position);
} else {
gridView = convertView;
}
return gridView;
}
答案 0 :(得分:1)
根据定义,ConvertView被重用。确保更改视图项的值膨胀或设置它。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// LayoutInflator to call external grid_item.xml file
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = inflater.inflate(R.layout.grid_item, null);
} else {
gridView = convertView;
}
event = gridValues.get(position);
// TODO: set view values based on this event
return gridView;
}