与Picasso Android一起使用RecyclerView出错

时间:2015-01-20 16:46:13

标签: java android android-studio picasso

我有包含图片的recyclerview。

@Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        // Get element from your dataset at this position and replace the contents of the view
        // with that element
        if(mEntities.get(position).url.equals("kosong")) {
            Log.e("LOAD", "KOSONG " + position);
            viewHolder.getTextDataView().setText(mEntities.get(position).data);
        }
        else{
            Log.e("LOAD", "ISI " + position);
            Picasso.with(mContext).load(mEntities.get(position).url).skipMemoryCache().into(viewHolder.imageView);
            viewHolder.getTextDataView().setText(mEntities.get(position).data);          
        }
    }

我成功地将图像加载到recyclerview的正确列表中,但不知何故,它在该recyclerview中的另一个列表中重复。为什么?

感谢您的回答:)

1 个答案:

答案 0 :(得分:1)

根据我的理解,如果你的网址包含" kosong"你不应该显示任何图像吗?

但是,您并未清除其之前图像的图像视图。请记住,listviews / recycler视图会回收他们的视图。因此,如果您在imageview上显示图像,稍后(向上和向下滚动)图像视图将被回收,它将包含设置给它的最后一个图像。

请记住,当您不想使用以下内容(if(mEntities.get(position).url.equals("kosong")))时,请清除图像: viewHolder.imageView.setImageResource(android.R.color.transparent);

所以它应该是这样的:

if(mEntities.get(position).url.equals("kosong")) {
        Log.e("LOAD", "KOSONG " + position);
        viewHolder.getTextDataView().setText(mEntities.get(position).data);
        //we don't need to display an image here however it's possible that our listview contains another image from a recycled row. Let's clear it
        viewHolder.imageView.setImageResource(android.R.color.transparent);
    }
    else{
        Log.e("LOAD", "ISI " + position);
        Picasso.with(mContext).load(mEntities.get(position).url).skipMemoryCache().into(viewHolder.imageView);
        viewHolder.getTextDataView().setText(mEntities.get(position).data);          
    }