问题描述
我正在RecyclerView适配器上进行延迟加载.首先,我获取图像元数据(如果有的话),然后让毕加索下载实际图像.
I'm doing lazy loading on a RecyclerView adapter. First I fetch the image metadata (if it has any) and then let Picasso download the actual image.
public class PostHolder extends RecyclerView.ViewHolder implements Callback<Photo>{
private PostImageView cover;
private TextView content;
private long id;
public PostHolder(View itemView) {
super(itemView);
this.itemView.setOnClickListener(onClickListener);
content = (TextView) itemView.findViewById(R.id.post_content);
cover = (PostImageView) itemView.findViewById(R.id.post_image);
}
public void setPost(final Post post, int i){
cover.unsetImage();
this.id = post.getPhotoId();
itemView.setTag(i);
content.setText(post.getMessage());
if("photo".equals(post.getType()) || "video".equals(post.getType()) && id != 0L){
cache.get(id, this);
}else{
cover.setUrl(post.getImageUrl());
}
}
@Override
public void result(Photo result) {
if(result != null && result.getId() == PostHolder.this.id){
cover.setPhoto(result);
}
}
}
cache.get()
从缓存中加载我的元数据,结果用result()
返回.setPost()
在onBindViewHolder()
中被调用.现在,我遇到了每个人都喜欢的视口问题-我的ImageView
在切换到正确的图像之前会显示不同的图像.我知道Picasso可以正确处理此问题,并且我要检查自己的加载情况,在其中将持有人的ID与元数据ID进行比较.我已经花了几个小时了.互联网,您是我唯一的希望.
cache.get()
loads my metadata from the cache, the result is returned with result()
.setPost()
gets called in onBindViewHolder()
. Now I'm getting everyone's favorite viewholder issue - my ImageView
displays a different image before switching to the correct one. I know that Picasso correctly handles this and I have a check for my own loading where i compare the holder's id to the metadata id. I've spent a few hours on this already. Internet, you are my only hope.
推荐答案
它现在已修复,尽管我不确定为什么.我相信是
It's fixed now, though I'm not exactly sure why. I believe it's
Picasso
.with(getContext())
.cancelRequest(this);
在unsetImage()
内的
内,因为需要双重请求来加载它.
inside unsetImage()
because of the double request needed to load it.
我认为会发生什么:
-
Picasso
仍在加载上一张图像. - 调度了对当前图像元数据的调用.
-
Picasso
加载之前的图像.由于Picasso
不了解元数据,因此不会自动取消它. - 已加载元数据,并且
Picasso
开始加载当前图像.
Picasso
is still loading the previous image.- A call for the current image metadata is dispatched.
Picasso
loads the preVious image. SincePicasso
doesn't know about the metadata, it's not automatically canceled.- The metadata get's loaded, and
Picasso
starts loading the current image.
这篇关于RecyclerView延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!