RecyclerView: Async image-loading

前端 未结 7 812
被撕碎了的回忆
被撕碎了的回忆 2020-12-28 15:58

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 i

相关标签:
7条回答
  • 2020-12-28 16:35

    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 of bindViewHolder(VH holder, int position) using setDrawable(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 in ListView and other old components.

    0 讨论(0)
  • 2020-12-28 16:36

    What MLProgrammer says is quite correct.

    The solution, fortunately, it's easy: stop the recycling

    holder.setIsRecyclable(false);
    

    This choice has its consequences, since doing what I suggest above inhibits one of RecyclerView main purposes: recycling.

    • Your RecyclerView will be slower to scroll (a new Holder has to be created every time, inflating again from the resources);
    • your usage of memory will be bigger.
    0 讨论(0)
  • 2020-12-28 16:38

    I would add to the good practice:

    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.

    not to display nothing, but to display a loader image so to give the user a feedback that theres some processing going on in that view and in a little while it will see the results. Seeing just a blank view is not good practice, you need to give feedback to the user.

    0 讨论(0)
  • 2020-12-28 16:39

    you must cancel old request in "onBindViewHolder" method:

    try{
            ((SpecialOfferViewHolder)viewHolder).imageContainer.cancelRequest();
    }catch(Exception e) {
    
    }
    

    remember to save image container in the viewHolder:

    public void onResponse(ImageContainer response, boolean arg1) {
                    ((SpecialOfferViewHolder)viewHolder).imageContainer=response;
    

    }

    0 讨论(0)
  • 2020-12-28 16:44

    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:

    1. Store some unique identifier in the View Holder during onBindViewHolder (this happens synchronously so if VH is recycled this will be overwritten)
    2. Then load the image asynchronously (with AsynchTask, RxJava, etc) and pass this unique id in the async call for reference
    3. 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

    0 讨论(0)
  • 2020-12-28 16:45

    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.

    0 讨论(0)
提交回复
热议问题