问题
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()
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.
回答1:
It's fixed now, though I'm not exactly sure why. I believe it's
Picasso
.with(getContext())
.cancelRequest(this);
inside unsetImage()
because of the double request needed to load it.
What I think happens:
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.
来源:https://stackoverflow.com/questions/27460779/recyclerview-lazy-loading