這是RecyclerView
緩存機制系列文章的第四篇材蛛,系列文章的目錄如下:
- RecyclerView緩存機制(咋復(fù)用深碱?)
- RecyclerView緩存機制(回收些啥?)
- RecyclerView緩存機制(回收去哪?)
- RecyclerView緩存機制(scrap view)
第一篇中遺留的一個問題還沒有解決:復(fù)用表項時優(yōu)先級最高的scrap view
是用來干嘛的?這篇文章試著通過閱讀源碼來解答這個問題毅整。
scrap view
對應(yīng)的存儲結(jié)構(gòu)是final ArrayList<ViewHolder> mAttachedScrap = new ArrayList<>();
。理解成員變量用途的最好辦法是 “搜索它在什么時候被訪問” 绽左。對于列表結(jié)構(gòu)來說就相當于 1. 在什么時候往列表添加內(nèi)容悼嫉? 2. 在什么時候清空列表內(nèi)容?
添加內(nèi)容
全局搜索mAttachedScrap
被訪問的地方拼窥,其中只有一處調(diào)用了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);
}
}
}
沿著調(diào)用鏈繼續(xù)往上:
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);
}
}
}
根據(jù)viewHolder
的不同狀態(tài)总棵,要么將其將其添加到mAttachedScrap
集合,要么將其存入回收池改含。其中recycleViewHolderInternal()
在RecyclerView緩存機制(回收去哪情龄?)分析過。
沿著調(diào)用鏈繼續(xù)向上:
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
中候味,回收數(shù)據(jù)的來源是LayoutManager
的孩子,而LayoutManager
的孩子都是屏幕上可見的表項隔心。 - 注釋中“暫時將當可見表項進行分離并回收”白群,既然是“暫時回收”,那待會必然會發(fā)生“復(fù)用”硬霍。復(fù)用邏輯可移步RecyclerView緩存機制(咋復(fù)用帜慢?)
- 至此可以得出結(jié)論:
mAttachedScrap
用于屏幕中可見表項的回收和復(fù)用
清空內(nèi)容
全局搜索mAttachedScrap
被訪問的地方,其中只有一處調(diào)用了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狀態(tài)
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();
}
}
}
沿著調(diào)用鏈向上:
public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild2 {
//RecyclerView布局的最后一步
private void dispatchLayoutStep3() {
...
mLayout.removeAndRecycleScrapInt(mRecycler);
...
}
至此可以得出結(jié)論:mAttachedScrap
生命周期起始于RecyclerView
布局開始唯卖,終止于RecyclerView
布局結(jié)束粱玲。
總結(jié)
經(jīng)過四篇文章的分析,RecyclerVeiw
的四級緩存都分析完了拜轨,總結(jié)如下:
Recycler
有4個層次用于緩存ViewHolder
對象抽减,優(yōu)先級從高到底依次為ArrayList<ViewHolder> mAttachedScrap
、ArrayList<ViewHolder> mCachedViews
橄碾、ViewCacheExtension mViewCacheExtension
卵沉、RecycledViewPool mRecyclerPool
。如果四層緩存都未命中法牲,則重新創(chuàng)建并綁定ViewHolder
對象-
緩存性能:
緩存 重新創(chuàng)建 ViewHolder
重新綁定數(shù)據(jù) mAttachedScrap false false mCachedViews false false mRecyclerPool false true
-
緩存容量:
-
mAttachedScrap
:沒有大小限制史汗,但最多包含屏幕可見表項。 -
mCachedViews
:默認大小限制為2拒垃,放不下時停撞,按照先進先出原則將最先進入的ViewHolder
存入回收池以騰出空間。 -
mRecyclerPool
:對ViewHolder
按viewType
分類存儲(通過SparseArray
)悼瓮,同類ViewHolder
存儲在默認大小為5的ArrayList
中戈毒。
-
-
緩存用途:
-
mAttachedScrap
:用于布局過程中屏幕可見表項的回收和復(fù)用艰猬。 -
mCachedViews
:用于移出屏幕表項的回收和復(fù)用,且只能用于指定位置的表項副硅,有點像“回收池預(yù)備隊列”姥宝,即總是先回收到mCachedViews
,當它放不下的時候恐疲,按照先進先出原則將最先進入的ViewHolder
存入回收池腊满。 -
mRecyclerPool
:用于移出屏幕表項的回收和復(fù)用,且只能用于指定viewType
的表項
-
-
緩存結(jié)構(gòu):
-
mAttachedScrap
:ArrayList<ViewHolder>
-
mCachedViews
:ArrayList<ViewHolder>
-
mRecyclerPool
:對ViewHolder
按viewType
分類存儲在SparseArray<ScrapData>
中培己,同類ViewHolder
存儲在ScrapData
中的ArrayList
中
-