Gridview scrolling issue using Android Volley Network library
I am using a GridView
to display some rows of images from a server. For async image loading I use the new networking library Volley. Initially the image loading works fine, but as soon as I scroll up and down the images are displayed randomly within wrong grid cells. However the TextView
content is always displayed correctly.
My Adapter-Code:
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;
}
Using a little workaround Volley works just fine with my Gridview-Adapter. If the user has unlocked the badge I load it via Volley NetworkImageView. If not I will show a placeholder image.
...
Badge badge = (Badge) getItem(position);
if (badge != null) {
// check if user already unlocked this badge
if (badge.unlocked && Utils.isUrl(badge.imageUrl)) {
viewHolder.badgeImage.setImageUrl(badge.imageUrl,
ImageCacheManager.getInstance().getImageLoader());
} else {
// explicity set placeholder image to avoid messy image recycling issue
viewHolder.badgeImage.setImageUrl(badge.placeholderUrl,
ImageCacheManager.getInstance().getImageLoader());
}
viewHolder.badgeName.setText(badge.name);
}
...
I'm also using Volley's NetworkImageView in my ListAdapters. It works just fine for me there too.
链接地址: http://www.djcxy.com/p/50600.html