简体   繁体   中英

RecyclerView and Picasso images disappear after scrolling

I didn't find an answer here , here and here .

I have an activity that shows list of posts (with or without images). When I scroll down and scroll up or refresh the list using SwipeRefreshLayout some of the images may disapper. I use RecyclerView to show list of posts and Picasso to load images. Here is my adapter binding:

@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
    // <...>
    if (item.getPhoto() != null) {
        Picasso.with(context)
                .load(item.getPhoto())
                .into(holder.mPostPhoto);
    } else {
        holder.mPostPhoto.setImageDrawable(null);
        holder.mPostPhoto.setVisibility(View.GONE);
    }
}

I send HTTP request to get posts and when I have new data I call PostsAdapter :

public void addAll(List<PostResponse> items) {
    this.items.clear();
    this.items.addAll(items);

    notifyDataSetChanged();
}

In MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // <...>
    mPostAdapter = new PostAdapter();
    mPosts.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    mPosts.setAdapter(mPostAdapter);

    mPostsSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            updatePosts();
        }
    });

    updatePosts();
}

private void updatePosts() {
    new Api(this).getPosts(new GetPostsCallback(this) {
        @Override
        public void onSuccess(final Paging<PostResponse> paging) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mPostAdapter.addAll(paging.getData());
                    mPostsSwipeRefresh.setRefreshing(false);
                }
            });
        }
    });
}

I find it's pretty basic, I don't understand why images disappear time after time. My list is not long and images resized before the upload to the server, they shouldn't use much memory. And the worst, when they disappear, they don't reload. They may reload only after I scroll down and up...

  • Please explain me why it happens.
  • How can I fix this problem?

Thanks!

I ran into this issue as well, but didn't want to disable the recycling. I found that by explicitly setting the ImageView visibility to View.VISIBLE before making the call to Picasso, the images loaded even after scrolling:

holder.image.setVisibility(View.VISIBLE);
Picasso.with(context)
    .load(imgUrl)
    .into(holder.image);

So, apparently RecyclerView was recycling items from my list and for some reason it couldn't reload images after that. Good question "why?"... Maybe because I did something wrong, not sure. This helped me:

recyclerView.getRecycledViewPool().setMaxRecycledViews(0, 0);

Basically you are turning off items recycling. It works for me because I don't render huge lists of items.

If you are using if condition in adapter then try below code

    if (model.image == null || model.equals(""))
        viewHolder.ivIndieImage.setVisibility(View.GONE);
    else{
        viewHolder.ivIndieImage.setVisibility(View.VISIBLE);
      }

try to setVisibility visible of the image.

You missed one thing in the onBindViewHolder. In the else part you are changing the visibility to View.GONE but in the If part you are not making it visible again. So, in recycler view the views are getting recycled means there data is getting changed but If you modify any property it will remain same for that view and it will be seen in the other items that are being shown using the same recycled view.

我能够通过删除相同的调整大小和中心裁切方法来解决此问题,以某种方式它们占用了过多的内存,这导致混乱了图像加载过程

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM