RecyclerView延迟加载 [英] RecyclerView lazy loading

查看:420
本文介绍了RecyclerView延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在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.

我认为会发生什么:

  1. Picasso仍在加载上一张图像.
  2. 调度了对当前图像元数据的调用.
  3. Picasso加载之前的图像.由于Picasso不了解元数据,因此不会自动取消它.
  4. 已加载元数据,并且Picasso开始加载当前图像.
  1. Picasso is still loading the previous image.
  2. A call for the current image metadata is dispatched.
  3. Picasso loads the preVious image. Since Picasso doesn't know about the metadata, it's not automatically canceled.
  4. The metadata get's loaded, and Picasso starts loading the current image.

这篇关于RecyclerView延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆