RecyclerView快取機制(scrap view)

唐子玄發表於2019-02-20

這是RecyclerView快取機制系列文章的第四篇,系列文章的目錄如下:

  1. RecyclerView快取機制(咋複用?)
  2. RecyclerView快取機制(回收些啥?)
  3. RecyclerView快取機制(回收去哪?)
  4. RecyclerView快取機制(scrap view)

第一篇中遺留的一個問題還沒有解決:複用表項時優先順序最高的scrap view是用來幹嘛的?這篇文章試著通過閱讀原始碼來解答這個問題。

scrap view對應的儲存結構是final ArrayList<ViewHolder> mAttachedScrap = new ArrayList<>();。理解成員變數用途的最好辦法是 “搜尋它在什麼時候被訪問” 。對於列表結構來說就相當於 1. 在什麼時候往列表新增內容? 2. 在什麼時候清空列表內容?

新增內容

全域性搜尋mAttachedScrap被訪問的地方,其中只有一處呼叫了mAttachedScrap.add():

public final class Recycler {
        /**
         * Mark an attached view as scrap.
         * 回收ViewHolder到scrap集合(mAttachedScrap或mChangedScrap)
         *
         * <p>"Scrap" views are still attached to their parent RecyclerView but are eligible
         * for rebinding and reuse. Requests for a view for a given position may return a
         * reused or rebound scrap view instance.</p>
         * scrap view依然依附於它的父親。。。
         *
         * @param view View to scrap
         */
        void scrapView(View view) {
            final ViewHolder holder = getChildViewHolderInt(view);
            if (holder.hasAnyOfTheFlags(ViewHolder.FLAG_REMOVED | ViewHolder.FLAG_INVALID)
                    || !holder.isUpdated() || canReuseUpdatedViewHolder(holder)) {
                if (holder.isInvalid() && !holder.isRemoved() && !mAdapter.hasStableIds()) {
                    throw new IllegalArgumentException("Called scrap view with an invalid view."
                            + " Invalid views cannot be reused from scrap, they should rebound from"
                            + " recycler pool." + exceptionLabel());
                }
                holder.setScrapContainer(this, false);
                //新增到mAttachedScrap集合中
                mAttachedScrap.add(holder);
            } else {
                if (mChangedScrap == null) {
                    mChangedScrap = new ArrayList<ViewHolder>();
                }
                holder.setScrapContainer(this, true);
                //新增到mChangedScrap集合中
                mChangedScrap.add(holder);
            }
        }
}
複製程式碼

沿著呼叫鏈繼續往上:

public abstract static class LayoutManager {
        private void scrapOrRecycleView(Recycler recycler, int index, View view) {
            final ViewHolder viewHolder = getChildViewHolderInt(view);
            if (viewHolder.shouldIgnore()) {
                if (DEBUG) {
                    Log.d(TAG, "ignoring view " + viewHolder);
                }
                return;
            }
            //刪除表項併入回收池
            if (viewHolder.isInvalid() && !viewHolder.isRemoved()
                    && !mRecyclerView.mAdapter.hasStableIds()) {
                removeViewAt(index);
                recycler.recycleViewHolderInternal(viewHolder);
            }
            //detach表項併入scrap集合
            else {
                detachViewAt(index);
                recycler.scrapView(view);
                mRecyclerView.mViewInfoStore.onViewDetached(viewHolder);
            }
        }
}
複製程式碼

根據viewHolder的不同狀態,要麼將其新增到mAttachedScrap集合,要麼將其存入回收池。其中recycleViewHolderInternal()RecyclerView快取機制(回收去哪?)分析過。 沿著呼叫鏈繼續向上:

public abstract static class LayoutManager {
        /**
         * Temporarily detach and scrap all currently attached child views. Views will be scrapped
         * into the given Recycler. The Recycler may prefer to reuse scrap views before
         * other views that were previously recycled.
         * 暫時將當可見表項進行分離並回收
         *
         * @param recycler Recycler to scrap views into
         */
        public void detachAndScrapAttachedViews(Recycler recycler) {
            final int childCount = getChildCount();
            //遍歷所有可見表項並回收他們
            for (int i = childCount - 1; i >= 0; i--) {
                final View v = getChildAt(i);
                scrapOrRecycleView(recycler, i, v);
            }
        }
       /**
         * Lay out all relevant child views from the given adapter.
         * 從給定的adapter佈局所有的孩子
         */
        public void onLayoutChildren(Recycler recycler, State state) {
            ...
            //在填充表項之前回收所有表項
            detachAndScrapAttachedViews(recycler);
            ...
            if (mAnchorInfo.mLayoutFromEnd) {
                ...
                //填充表項
                fill(recycler, mLayoutState, state, false);
                ...
            }
            ...
        }
}

public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild2 {
    //RecyclerView佈局的第二步
    private void dispatchLayoutStep2() {
        ...
        mLayout.onLayoutChildren(mRecycler, mState);
        ...
    }
}
複製程式碼
  • 在將表項一個個填充到列表之前會先將其先回收到mAttachedScrap中,回收資料的來源是LayoutManager的孩子,而LayoutManager的孩子都是螢幕上可見的表項。
  • 註釋中“暫時將當可見表項進行分離並回收”,既然是“暫時回收”,那待會必然會發生“複用”。複用邏輯可移步RecyclerView快取機制(咋複用?)
  • 至此可以得出結論:mAttachedScrap用於螢幕中可見表項的回收和複用

清空內容

全域性搜尋mAttachedScrap被訪問的地方,其中只有一處呼叫了mAttachedScrap.clear():

public final class Recycler {
        void clearScrap() {
            mAttachedScrap.clear();
            if (mChangedScrap != null) {
                mChangedScrap.clear();
            }
        }
}

public abstract static class LayoutManager {
        /**
         * Recycles the scrapped views.
         * 回收所有scrapped view
         */
        void removeAndRecycleScrapInt(Recycler recycler) {
            final int scrapCount = recycler.getScrapCount();
            // Loop backward, recycler might be changed by removeDetachedView()
            //遍歷搜有scrap view並重置ViewHolder狀態
            for (int i = scrapCount - 1; i >= 0; i--) {
                final View scrap = recycler.getScrapViewAt(i);
                final ViewHolder vh = getChildViewHolderInt(scrap);
                if (vh.shouldIgnore()) {
                    continue;
                }
                vh.setIsRecyclable(false);
                if (vh.isTmpDetached()) {
                    mRecyclerView.removeDetachedView(scrap, false);
                }
                if (mRecyclerView.mItemAnimator != null) {
                    mRecyclerView.mItemAnimator.endAnimation(vh);
                }
                vh.setIsRecyclable(true);
                recycler.quickRecycleScrapView(scrap);
            }
            //清空scrap view集合
            recycler.clearScrap();
            if (scrapCount > 0) {
                mRecyclerView.invalidate();
            }
        }
}
複製程式碼

沿著呼叫鏈向上:

public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild2 {
    //RecyclerView佈局的最後一步
    private void dispatchLayoutStep3() {
        ...
        mLayout.removeAndRecycleScrapInt(mRecycler);
        ...
}
複製程式碼

至此可以得出結論:mAttachedScrap生命週期起始於RecyclerView佈局開始,終止於RecyclerView佈局結束。

總結

經過四篇文章的分析,RecyclerVeiw的四級快取都分析完了,總結如下:

  1. Recycler有4個層次用於快取ViewHolder物件,優先順序從高到底依次為ArrayList<ViewHolder> mAttachedScrapArrayList<ViewHolder> mCachedViewsViewCacheExtension mViewCacheExtensionRecycledViewPool mRecyclerPool。如果四層快取都未命中,則重新建立並繫結ViewHolder物件

  2. 快取效能:

    快取 重新建立ViewHolder 重新繫結資料
    mAttachedScrap false false
    mCachedViews false false
    mRecyclerPool false true
  3. 快取容量:

    • mAttachedScrap:沒有大小限制,但最多包含螢幕可見表項。
    • mCachedViews:預設大小限制為2,放不下時,按照先進先出原則將最先進入的ViewHolder存入回收池以騰出空間。
    • mRecyclerPool:對ViewHolderviewType分類儲存(通過SparseArray),同類ViewHolder儲存在預設大小為5的ArrayList中。
  4. 快取用途:

    • mAttachedScrap:用於佈局過程中螢幕可見表項的回收和複用。
    • mCachedViews:用於移出螢幕表項的回收和複用,且只能用於指定位置的表項,有點像“回收池預備佇列”,即總是先回收到mCachedViews,當它放不下的時候,按照先進先出原則將最先進入的ViewHolder存入回收池。
    • mRecyclerPool:用於移出螢幕表項的回收和複用,且只能用於指定viewType的表項
  5. 快取結構:

    • mAttachedScrapArrayList<ViewHolder>
    • mCachedViewsArrayList<ViewHolder>
    • mRecyclerPool:對ViewHolderviewType分類儲存在SparseArray<ScrapData>中,同類ViewHolder儲存在ScrapData中的ArrayList

相關文章