緩存機制
ListView的兩級緩存
ListView的緩存和復用由它的父類AbsListView中的RecycleBin實現,設了兩個緩存數組mActiveViews和mScrapViews。mActiveViews緩存顯示在屏幕中的view,mScrapViews按ViewType緩存離屏的view
class RecycleBin { /** * The position of the first view stored in mActiveViews. */ private int mFirstActivePosition; /** * Views that were on screen at the start of layout. This array is populated at the start of * layout, and at the end of layout all view in mActiveViews are moved to mScrapViews. * Views in mActiveViews represent a contiguous range of Views, with position of the first * view store in mFirstActivePosition. */ private View[] mActiveViews = new View[0]; /** * Unsorted views that can be used by the adapter as a convert view. */ private ArrayList<View>[] mScrapViews; }
RecyclerView的四級緩存
RecyclerView的緩存和復用由Recycler實現,mAttachedScrap和mCachedViews的緩存方式跟ListView相似。mRecyclerPool是多個RecyclerView的復用池,mViewCacheExtension不直接使用,需要用戶再定制,默認不實現。
public final class Recycler { final ArrayList<ViewHolder> mAttachedScrap = new ArrayList<>(); final ArrayList<ViewHolder> mCachedViews = new ArrayList<ViewHolder>(); RecycledViewPool mRecyclerPool; private ViewCacheExtension mViewCacheExtension; }
緩存內容
- ListView緩存View
- RecyclerView緩存ViewHolder,抽象可理解為:View + ViewHolder(避免每次createView時調用findViewById)
總結
ListView二級緩存
RecyclerView四級緩存