我正在使用 aGridView
来显示来自服务器的一些图像行。对于异步图像加载,我使用新的网络库Volley。
最初图像加载工作正常,但只要我向上和向下滚动,图像就会随机显示在错误的网格单元格中。但是,TextView
内容始终正确显示。
我的适配器代码:
class BadgeGalleryAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ImageLoader mImageLoader;
// Constructor
public BadgeGalleryAdapter(Context c) {
mInflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mImageLoader = ImageCacheManager.getInstance().getImageLoader();
}
@Override
public int getCount() {
return mBadgeList.size();
}
@Override
public Object getItem(int position) {
return mBadgeList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
// Use ViewHolder pattern to only inflate once
if (convertView == null) {
convertView = mInflater.inflate(R.layout.user_badge_grid_item,
parent, false);
viewHolder = new ViewHolder();
viewHolder.badgeImage = (ImageView) convertView
.findViewById(R.id.grid_badge_image);
viewHolder.badgeName = (TextView) convertView
.findViewById(R.id.grid_badge_name);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Badge badge = (Badge) getItem(position);
if (badge != null) {
//using Volley ImageLoader
mImageLoader.get(badge.imageUrl, ImageLoader
.getImageListener(viewHolder.badgeImage, 0, 0));
viewHolder.badgeName.setText(badge.name);
}
return convertView;
}
}
static class ViewHolder {
ImageView badgeImage;
TextView badgeName;
}