Recyclerview: Async Image-loading
Solution 1:
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:
In the good practice you set your
ImageView
to display nothing at the beginning ofbindViewHolder(VH holder, int position)
usingsetDrawable(null)
or similar.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 inListView
and other old components.
Solution 2:
You should check the universal image loader. It has memory cache, disk cache and it loads your images asynchronously so doesn't block the ui. You can set default image and/or failed to fetch image etc. It can sample down your image to decrease the memory footprint of the bitmap. I really recommend you to use it for images.
Do not disable recyclable for your case because it is pointless. Images must be recycled because their bitmap drawables generate very high memory overload if not properly sampled.
Sample usage in RecyclerViewAdapter:
@OverridepublicvoidonBindViewHolder(CustomViewHolder viewHolder, int position) {
StringimageUri="";//local or remote image uri address//viewHolder.imgView: reference to your imageview//before you call the displayImage you have to //initialize imageloader in anywhere in your code for once. //(Generally done in the Application class extender.)
ImageLoader.getInstance().displayImage(imageUri, viewHolder.imgView);
}
edit: Nowadays, I consider Glide as my main image loading and caching library. You can use it like this:
Glide.with(context)
.load(imageUri)
.placeholder(R.drawable.myplaceholder)
.into(imageView);
Solution 3:
You should cancel the old request before starting a new one, but regardless of cancelling you can still show the wrong image if both images loaded more or less at the same time on the same container/view holder that has been recycled (happens easily with fast scroll and small images).
The solution is to:
- Store some unique identifier in the View Holder during onBindViewHolder (this happens synchronously so if VH is recycled this will be overwritten)
- Then load the image asynchronously (with AsynchTask, RxJava, etc) and pass this unique id in the async call for reference
- Finally, in the image loaded method for post-processing (onPostExecute for AsyncTasks), check that the id passed in the async request is the same as the current id present in the View Holder.
Example loading icons from apps in background with RxJava:
public void loadIcon(final ImageView appIconView, final ApplicationInfo appInfo, final String uniqueAppID) {
Single.fromCallable(() -> {
return appIconView.getContext().getPackageManager().getApplicationIcon(appInfo);
})
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe( drawable -> {
if (uniqueAppID.equals(mUniqueAppID)) { // Show always the correct app icon
appIconView.setImageDrawable(drawable);
}
}
);
}
Here mUniqueAppID is a field of the view holder changed by onBindViewHolder
Solution 4:
you must cancel old request in "onBindViewHolder" method:
try{
((SpecialOfferViewHolder)viewHolder).imageContainer.cancelRequest();
}catch(Exception e) {
}
remember to save image container in the viewHolder:
publicvoidonResponse(ImageContainer response, boolean arg1) {
((SpecialOfferViewHolder)viewHolder).imageContainer=response;
}
Solution 5:
You should use Picasso. A powerful image downloading and caching library for Android. Easy to use and a powerful library. Using this library you can fetch images asynchronously or synchronously from Resources, assets, files, content providers.
Post a Comment for "Recyclerview: Async Image-loading"