RecyclerView:异步图像加载 [英] RecyclerView: Async image-loading

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

问题描述

我使用RecyclerView来显示包含imageView的列表.为了使用户界面更加流畅,我使用asyncTask将保存在SD卡上的58dp缩略图加载到了这些imageViews中.

Im using RecyclerView to display a list containing an imageView. To make the UI more fluently, I load 58dp thumbnails saved on sd card into these imageViews with an asyncTask.

问题是,一旦childView出现在视觉显示中,就会重新使用来自另一个数据的旧图像,然后在AsyncTask完成后将其替换.我可以通过将imageView位图设置为onPreExecute中的null来停止混洗.

The problem is, that once a childView comes in visual display, an old image from another data is being reused and then replaced once the AsyncTask finishes. I can stop the shuffling by setting the imageView bitmap to null in onPreExecute.

是否有一种方法可以真正重用旧图像,或者是否真的需要在每次使用新的View时从sd卡中加载图像?这使视图非常难看,因为要么首先出现错误的图像,要么图像是纯白色的.

Is there a way to really reuse old images or do I really have to load the images from sd-card each time a new View comes in place? This makes view quite ugly because either there are wrong images first or the image is plain white.

推荐答案

由于视图重用,您将获取已包含内容的视图,如果您使用的是ViewHolder模式,则在ListViews上也是一个问题,您应该这样做.

Due to view reuse you'll fetch views with content already on them, this was a problem on ListViews too if you were using the ViewHolder pattern, which you should.

这里有两种解决方案,好的做法和不好的破解:

There are two solutions here, the good practice and the bad hack:

  • 在优良作法中,您将ImageView设置为不显示任何内容. bindViewHolder(VH holder, int position)的开头,使用 setDrawable(null)或类似版本.

  • In the good practice you set your ImageView to display nothing at the beginning of bindViewHolder(VH holder, int position) using setDrawable(null) or similar.

在糟糕的情况下,您不会循环/重用视图,不会强制执行ViewHolder模式,并且每次都会对其进行充气,但这仅在ListView和其他旧组件中允许./p>

In the bad hack you wouldn't recycle/reuse views, not enforcing the ViewHolder pattern, and you'd inflate it every time, but that's only allowed in ListView and other old components.

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

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