RecyclerView's recycling and reuse strategy

Problem sorting

  1. When to recycle? When to reuse? (when)
  2. Recycle what? Reuse what? (what)
  3. Where does the recycling go? Where does the reuse come from? (where)
  4. What is the specific strategy for recycling and reuse? (how)

when

RecyclerView is a container that supports sliding, so the View inside it will disappear and display. This process will only happen in the state of sliding. Therefore, **recycling and reuse are all in the move event in RecyclerView's onTouchEvent Happened.** Specifically, recycling occurs when View changes from visible to invisible, and reuse occurs when View changes from invisible to visible.
The fill method is the entry point for recycling and reuse

what & where

Recycling and reuse in RecyclerView is realized through its internal class Recycler. ( Click here to view the source code )

public final class Recycler {
    
    
        final ArrayList<ViewHolder> mAttachedScrap = new ArrayList<>();
        ArrayList<ViewHolder> mChangedScrap = null;

        final ArrayList<ViewHolder> mCachedViews = new ArrayList<ViewHolder>();

        private final List<ViewHolder>
                mUnmodifiableAttachedScrap = Collections.unmodifiableList(mAttachedScrap);

        private int mRequestedCacheMax = DEFAULT_CACHE_SIZE;
        int mViewCacheMax = DEFAULT_CACHE_SIZE;

        RecycledViewPool mRecyclerPool;

        private ViewCacheExtension mViewCacheExtension;

        static final int DEFAULT_CACHE_SIZE = 2;

Observing its members, we can know:

  1. The object recycled and reused by Recycler is ViewHolder(the cache type is ArrayList of ViewHolder), notView
  2. It has four caches
module describe Scenes
mAttachedScrap Store the ViewHolder that is marked to be removed but may be reused (such as a View that slides out halfway) Hiding an entire RecyclerView, all items in it will have mAttachedScrap
mChangedScrap Stored data has changed, ViewHolder needs to be updated notifyItemChanged (index), if the current index is displayed on the screen, the ViewHodler of this index will be stored in mChangedScrap.
mCachedViews ViewHodler for caching the slide-out screen. A finite queue of ViewHolders that are completely removed from the screen. When it is full and new ones are added, the oldest ones will be moved into the mRecyclerPool first
mViewCacheExtension A helper class for developers to customize the View cache. It is necessary to define the View cache and recycling logic by themselves. uncommonly used.
mRecyclerPool Under the same Adapter, RecycledViewPool can share the View between multiple RecyclerViews. By default each RecyclerView creates one of its own; this can be set manually via setRecycledViewPool(). When mCachedViews is full, it will be placed in RecycledViewPool

Note:

  • One-to-one correspondence with the current RecyclerView: mAttachedScrap, mChangedScrap, mCachedViews
  • Multiple RecyclerViews may be shared: mViewCacheExtension, mRecyclerPool

How

Recycle

Process ( source code ):
recycling process

reuse

Process ( source code ):
Multiplexing process

Cache acquisition order:
Cache acquisition order

  • **Only execute bindViewHolder() from RecycledViewPool. Obtain ViewHolder from other caches and return directly.**
  • CreateViewHolder() will only be created when the fourth-level cache is not available

mCachedViews

  • Its size can be set by setItemViewCacheSize().
  • The default is 2.

RecycledViewPool

  • It looks for ViewHolder according to ViewType
  • Each ViewYype stores up to 5 by default
  • The returned ViewHolder needs to re-execute bindViewHolder() to update the content

Cache

By default, RecyclerView can store:
N (the maximum number of items that can be displayed on the screen) + 2 (size of mCachedViews) + 5*M (number of ViewTypes)

Reference Materials
Sequence Diagram Source
Cache Detailed Explanation 1
Cache Explanation 2

Guess you like

Origin blog.csdn.net/Reven_L/article/details/120311066