Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 737
Andrei Avatar asked Nov 05 '16 17:11

Andrei


2 Answers

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);
like image 87
William Brawner Avatar answered Oct 17 '22 21:10

William Brawner


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.

like image 40
Andrei Avatar answered Oct 17 '22 21:10

Andrei