我有一个产品目录CustomGrid
。 Grid的数据存储在本地。 Text来自本地JSON文件,图像也存储在res
下。
在显示GridView时,300项未正确排列。它们在20个条目后重复,当我向上和向下滚动时它们会混淆。
我调试了数据数组,但它们看起来很好。我现在每个项目也有相同的图像路径。因此每个Gridview项都具有相同的图像。但那就是GridView没有不同的行为。
我在其他Gridview上没有问题,项目少得多。我读到了延迟加载,但我没有下载图像文件
为什么Grid表现得像这样?这一次是否过载?我怎么解决这个问题?
编辑:
CustomGrid:
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] gridItemText;
private final int[] imageID;
public CustomGrid(Context c, String[] gridItemText, int[] imageID){
mContext = c;
this.gridItemText = gridItemText;
this.imageID = imageID;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return gridItemText.length;
}
@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) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
//grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single_main, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_single_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_single_image);
textView.setText(gridItemText[position]);
imageView.setImageResource(imageID[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
的活动:
CustomGrid adapter = new CustomGrid(ProductCatalogActivity.this, menuData, imagePath);
gridView1=(GridView)findViewById(R.id.grid_Main);
gridView1.setAdapter(adapter);
gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//ToDo
}
});
答案 0 :(得分:1)
尝试使用此代替您的Viewholder机制
TextView textView;
ImageView imageView;
if (convertView == null) {
//grid = new View(mContext);
convertView = inflater.inflate(R.layout.grid_single_main, null);
textView = (TextView) convertView.findViewById(R.id.grid_single_text);
imageView = (ImageView) convertView.findViewById(R.id.grid_single_image);
convertView.setTag(new ViewHolder(textView, imageView));
} else {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
textView = viewHolder.textView;
imageView = viewHolder.imageView;
}
textView.setText(gridItemText[position]);
imageView.setImageResource(imageID[position]);
return convertView;
也使用此视图
private static class ViewHolder {
public final TextView textView;
public final ImageView imageView;
public ViewHolder(TextView textView, ImageView imageView) {
this.textView = textView;
this.imageView = imageView;
}
}