我没有找到答案here , here和 here .
我有一个显示帖子列表的 Activity (有或没有图像)。当我使用 SwipeRefreshLayout
向下滚动并向上滚动或刷新列表时一些图像可能会消失。我用 RecyclerView
显示帖子列表和Picasso
加载图像。这是我的适配器绑定(bind):
@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
:public void addAll(List<PostResponse> items) {
this.items.clear();
this.items.addAll(items);
notifyDataSetChanged();
}
在 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);
}
});
}
});
}
我觉得这很基本,我不明白为什么图像会一次又一次地消失。我的列表不长,并且在上传到服务器之前调整了图像大小,它们不应该使用太多内存。最糟糕的是,当它们消失时,它们不会重新加载。只有在我向下和向上滚动后,它们才能重新加载...
谢谢!
最佳答案
我也遇到了这个问题,但不想禁用回收。我发现通过将 ImageView 可见性显式设置为 View.VISIBLE
在调用 picasso 之前,即使在滚动之后也会加载图像:
holder.image.setVisibility(View.VISIBLE);
Picasso.with(context)
.load(imgUrl)
.into(holder.image);
关于android - RecyclerView 和 Picasso 图像在滚动后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40441176/