我没有找到答案here https://stackoverflow.com/questions/29564824/recyclerview-disappearing-images, here https://stackoverflow.com/questions/27743339/strange-behaviour-of-images-in-recyclerview and here https://stackoverflow.com/questions/36819235/android-image-disappear-in-recyclerview-after-scrolling-list-up-and-down.
我有一个显示帖子列表的活动(带或不带图像)。当我向下滚动并向上滚动或刷新列表时SwipeRefreshLayout
一些图像可能会消失。我用RecyclerView
显示帖子列表和Picasso
加载图像。这是我的适配器绑定:
@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();
}
在主要活动中:
@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);
}
});
}
});
}
我发现这很基本,我不明白为什么图像一次又一次消失。我的列表不长,图像在上传到服务器之前调整了大小,它们不应该使用太多内存。最糟糕的是,当它们消失时,它们不会重新加载。只有在我向下和向上滚动后,它们才可能重新加载......
- 请解释一下为什么会发生这种情况。
- 我该如何解决这个问题?
Thanks!