【问题标题】:Error RecyclerView with Picasso AndroidPicasso Android 的错误 RecyclerView
【发布时间】:2015-01-21 00:52:46
【问题描述】:

我有包含图片的 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 的另一个列表中重复。为什么?

感谢您的回答:)

【问题讨论】:

  • 我确保之前没有错误的 if else 条件,但不知何故它仍然在随机列表中复制图像。

标签: java android android-studio picasso


【解决方案1】:

所以据我了解,如果您的网址包含“kosong”,您不应该显示任何图像吗?

但是,您并没有清除之前图像的图像视图。请记住,列表视图/回收器视图会回收它们的视图。因此,如果您在 imageview 上显示图像,然后(向上和向下滚动)该 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);          
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多