RecyclerView 和 Picasso 图像在滚动后消失 [英] RecyclerView and Picasso images disappear after scrolling

查看:31
本文介绍了RecyclerView 和 Picasso 图像在滚动后消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有在此处找到答案,此处此处.

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

我有一个活动显示帖子列表(带或不带图像).当我使用 SwipeRefreshLayout 向下滚动并向上滚动或刷新列表时,某些图像可能会消失.我使用 RecyclerView 显示帖子列表和 Picasso 加载图像.这是我的适配器绑定:

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);
    }
}

我发送 HTTP 请求来获取帖子,当我有新数据时,我调用 PostsAdapter:

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();
}

在主活动中:

@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...

  • 请解释为什么会发生这种情况.
  • 我该如何解决这个问题?

谢谢!

推荐答案

我也遇到了这个问题,但不想禁用回收.我发现通过在调用 Picasso 之前将 ImageView 可见性显式设置为 View.VISIBLE ,即使在滚动后加载图像:

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);

这篇关于RecyclerView 和 Picasso 图像在滚动后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆