使用ImageLoader库时,GridView中的项重复

时间:2012-09-09 14:21:14

标签: android android-gridview universal-image-loader

我正在使用GridView来显示ImageView的列表。图像通过ImageLoader库从Internet加载。我还实现了一个自定义适配器(扩展BaseAdapter):

public class ImagePreviewAdapter extends BaseAdapter {

    private Context mContext;

    private List<LastPost> mPosts = new ArrayList<LastPost>();

    private ImageLoader mImageLoader;

    private ImageLoadingListener mLoadingListener;

    public ImagePreviewAdapter(Context context, List<LastPost> posts, ImageLoadingListener listener) {
        this.mContext = context;
        this.mPosts = posts;
        this.mLoadingListener = listener;

        Activity activity = (Activity)mContext;
        this.mImageLoader = ((MeanwhileInRussia)activity.getApplication()).getImageLoader();
    }

    public int getCount() {
        return mPosts.size();
    }

    public LastPost getItem(int position) {
        return mPosts.get(position);
    }

    public long getItemId(int position) {
        LastPost post = mPosts.get(position);
        return Long.parseLong(post.getId());
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;

        // if it's not recycled, initialize some attributes
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(125, 125));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(0, 0, 0, 0);

            String imageLink = mPosts.get(position).getImageurl();
            DisplayImageOptions displayImgOptions = ImageLoaderConfigUtil
                .getDisplayImageOptions(mContext);

            if (mLoadingListener != null) {
                mImageLoader
                    .displayImage(imageLink, imageView, displayImgOptions, mLoadingListener);
           } else {
                mImageLoader.displayImage(imageLink, imageView, displayImgOptions);
           }

        } else {
            ImageView restoringView = (ImageView)convertView;
            imageView = restoringView;
        }

        return imageView;
    }
}

每次系统调用getView()方法时,我都会创建一个新的ImageView对象并要求ImageLoader将图像放入其中(如果当然是convertView == null)。对于URL的第一部分,一切正常。但我的问题从第二部分开始:GridView中没有新图像,只是第一部分的副本......我意识到问题隐藏在convertView == null的某处:对于所有理论上的新元素这个检查总是等于FALSE!而不是创建新对象并从URL加载图像我的代码将一些OLD对象放入新的位置。有趣的事实:getItemId(position)方法为我的LastPost对象返回正确的ID。这是否意味着我只是在适配器中有正确的LastPosts对象的错误视图?

有什么建议吗?谢谢!

2 个答案:

答案 0 :(得分:3)

试试这个......

public class ImagePreviewAdapter extends BaseAdapter 
{

private Context mContext;

private List<LastPost> mPosts = new ArrayList<LastPost>();

private ImageLoader mImageLoader;

private ImageLoadingListener mLoadingListener;

/**
 * Constructor
 * 
 * @param context
 * @param posts
 * @param listener - using for showing loading progress
 */
public ImagePreviewAdapter(Context context, List<LastPost> posts, ImageLoadingListener listener) {
    this.mContext = context;
    this.mPosts = posts;
    this.mLoadingListener = listener;

    Activity activity = (Activity)mContext;
    this.mImageLoader = ((MeanwhileInRussia)activity.getApplication()).getImageLoader();
}

public int getCount() {
    return mPosts.size();
}

public LastPost getItem(int position) {
    return mPosts.get(position);
}

public long getItemId(int position) {
    LastPost post = mPosts.get(position);
    return Long.parseLong(post.getId());
}

/*
 * Create a new ImageView for each item referenced by the Adapter
 */
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;

    // if it's not recycled, initialize some attributes
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(125, 125));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(0, 0, 0, 0);

    } 
   else 
      {
        ImageView restoringView = (ImageView)convertView;
        imageView = restoringView;
     }
        final String imageLink = mPosts.get(position).getImageurl();
        final DisplayImageOptions displayImgOptions = ImageLoaderConfigUtil
            .getDisplayImageOptions(mContext);

   if (mLoadingListener != null) 
    {
       mImageLoader.displayImage(imageLink, imageView,displayImgOptions,mLoadingListener);
       }
     else 
        {
            mImageLoader.displayImage(imageLink, imageView, displayImgOptions);
       }

    return imageView;
}
}

答案 1 :(得分:1)

试试这个

public View getView(int position, View convertView, ViewGroup parent) {

   ViewHolder holder = null;
    // if it's not recycled, initialize some attributes
    if (convertView == null) {
        holder = new ViewHolder();
        holder.image = new ImageView(mContext);
        holder.image.setLayoutParams(new GridView.LayoutParams(125, 125));
        holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        holder.image.setPadding(0, 0, 0, 0);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    String imageLink = mPosts.get(position).getImageurl();
    DisplayImageOptions displayImgOptions = ImageLoaderConfigUtil.getDisplayImageOptions(mContext);
    mImageLoader.displayImage(imageLink, holder.image, displayImgOptions);

    return convertView;
}


protected class ViewHolder{
    ImageView image;
}