RecyclerView延迟加载(RecyclerView lazy loading)

2019-10-21 10:34发布

我是一个RecyclerView适配器上做延迟加载。 首先,我获取的图像元数据(如果有任何),然后让毕加索下载的实际图像。

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() 现在,我得到了大家的喜爱viewholder问题-我ImageView显示切换到正确的人之前不同的图像。 我知道毕加索正确处理这一点,我有我自己的加载检查其中i持有人的ID进行比较,以元数据ID。 我花了几个小时,在这了。 互联网上,你是我唯一的希望。

Answer 1:

它现在是固定的,虽然我不知道是什么原因。 我相信这是

Picasso
    .with(getContext())
    .cancelRequest(this);

内部unsetImage()因为所需要的双重请求加载它。

我认为发生的情况:

  1. Picasso仍在加载以前的图像。
  2. 用于当前图像的元数据调用时调度。
  3. Picasso加载一张图像。 由于Picasso不知道有关的元数据,它不会自动取消。
  4. 元数据得到的加载,和Picasso开始加载当前图像。


文章来源: RecyclerView lazy loading