资讯专栏INFORMATION COLUMN

RecyclerView源码分析(一) Recycler

klivitamJ / 2256人阅读

摘要:在,,中调用在,,,,,具体的回收操作,在类的方法中,其中这两个方法,,方法利用获取与之绑定的方法回收有两种情况当满了,删除中存放的一个,把这个放入到中放入中,的增和删都在方法中查删在方法中的删查在方法只在其他没有的方法中调用

dispatchLayout在onLayout,
consumePendingUpdateOperations,
resumeRequestLayout中调用

resumeRequestLayout 在focusSearch,onMeasure,
dispatchLayoutStep1,dispatchLayoutStep2,dispatchLayoutStep3,
ViewFlinger.run
removeAnimatingView
scrollByInternal<-- scrollBy

    void dispatchLayout() {
        if (mState.mLayoutStep == State.STEP_START) {
            dispatchLayoutStep1();
            mLayout.setExactMeasureSpecsFrom(this);
            dispatchLayoutStep2();
        } else if (mAdapterHelper.hasUpdates() || mLayout.getWidth() != getWidth() ||
                mLayout.getHeight() != getHeight()) {
            // First 2 steps are done in onMeasure but looks like we have to run again due to
            // changed size.
            mLayout.setExactMeasureSpecsFrom(this);
            dispatchLayoutStep2();
        } else {
            // always make sure we sync them (to ensure mode is exact)
            mLayout.setExactMeasureSpecsFrom(this);
        }
        dispatchLayoutStep3();
    }
     private void dispatchLayoutStep3() {
         ... ...
         mViewInfoStore.process(mViewInfoProcessCallback);
         ... ...
     }
    private final ViewInfoStore.ProcessCallback mViewInfoProcessCallback =
            new ViewInfoStore.ProcessCallback() {
            ... ...
            @Override
            public void unused(ViewHolder viewHolder) {
                  mLayout.removeAndRecycleView(viewHolder.itemView, mRecycler);
            }
    }

RecyclerView.LayoutManager:

       public void removeAndRecycleView(View child, Recycler recycler) {
            removeView(child);
            recycler.recycleView(child);
        }

具体的回收操作,在RecyclerView.Recycler类的recycleView方法中,
RecyclerView.Recycler:

    public void recycleView(View view) {
            // This public recycle method tries to make view recycle-able since layout manager
            // intended to recycle this view (e.g. even if it is in scrap or change cache)
            ViewHolder holder = getChildViewHolderInt(view);
            if (holder.isTmpDetached()) {
                removeDetachedView(view, false);
            }
            if (holder.isScrap()) {
                holder.unScrap();
            } else if (holder.wasReturnedFromScrap()){
                holder.clearReturnedFromScrapFlag();
            }
            recycleViewHolderInternal(holder);
     }
     
    其中这两个方法,getChildViewHolderInt,recycleViewHolderInternal
    getChildViewHolderInt方法利用View获取与之绑定的ViewHolder
    static ViewHolder getChildViewHolderInt(View child) {
        if (child == null) {
            return null;
        }
        return ((LayoutParams) child.getLayoutParams()).mViewHolder;
    }
    
    recycleViewHolderInternal方法回收ViewHolder,有两种情况:
     1.当mCachedViews满了,删除mCachedViews中存放的一个ViewHolder,把这个ViewHoler放入到RecyclerPool中;
     2.放入mCachedViews中
     
     void recycleViewHolderInternal(ViewHolder holder) {
         ... ...
           if (forceRecycle || holder.isRecyclable()) {
                if (!holder.hasAnyOfTheFlags(ViewHolder.FLAG_INVALID | ViewHolder.FLAG_REMOVED
                        | ViewHolder.FLAG_UPDATE)) {
                    // Retire oldest cached view
                    final int cachedViewSize = mCachedViews.size();
                    if (cachedViewSize == mViewCacheMax && cachedViewSize > 0) {
                        recycleCachedViewAt(0);
                    }
                    if (cachedViewSize < mViewCacheMax) {
                        mCachedViews.add(holder);
                        cached = true;
                    }
                }
                if (!cached) {
                    addViewHolderToRecycledViewPool(holder);
                    recycled = true;
                }
            } 
            ... ...
     }
     void recycleCachedViewAt(int cachedViewIndex) {
            if (DEBUG) {
                Log.d(TAG, "Recycling cached view at index " + cachedViewIndex);
            }
            ViewHolder viewHolder = mCachedViews.get(cachedViewIndex);
            if (DEBUG) {
                Log.d(TAG, "CachedViewHolder to be recycled: " + viewHolder);
            }
            addViewHolderToRecycledViewPool(viewHolder);
            mCachedViews.remove(cachedViewIndex);
     }
     
     void addViewHolderToRecycledViewPool(ViewHolder holder) {
            ViewCompat.setAccessibilityDelegate(holder.itemView, null);
            dispatchViewRecycled(holder);
            holder.mOwnerRecyclerView = null;
            getRecycledViewPool().putRecycledView(holder);
     }

RecyclerView.RecyclerPool:

    public void putRecycledView(ViewHolder scrap) {
            final int viewType = scrap.getItemViewType();
            final ArrayList scrapHeap = getScrapHeapForType(viewType);
            if (mMaxScrap.get(viewType) <= scrapHeap.size()) {
                return;
            }
            if (DEBUG && scrapHeap.contains(scrap)) {
                throw new IllegalArgumentException("this scrap item already exists");
            }
            scrap.resetInternal();
            scrapHeap.add(scrap);
    }

mCachedViews, RecyclerPool
mCachedViews的增和删都在recycleViewHolderInternal方法中,查删在getScrapViewForPosition方法中

    ViewHolder getScrapViewForPosition(int position, int type, boolean dryRun) {
        ... ... 
        final int cacheSize = mCachedViews.size();
            for (int i = 0; i < cacheSize; i++) {
                final ViewHolder holder = mCachedViews.get(i);
                // invalid view holders may be in cache if adapter has stable ids as they can be
                // retrieved via getScrapViewForId
                if (!holder.isInvalid() && holder.getLayoutPosition() == position) {
                    if (!dryRun) {
                        mCachedViews.remove(i);
                    }
                    if (DEBUG) {
                        Log.d(TAG, "getScrapViewForPosition(" + position + ", " + type +
                                ") found match in cache: " + holder);
                    }
                    return holder;
                }
         }
         ... ... 
    }

RecyclerPool的删查在
Recycler:

    View getViewForPosition(int position, boolean dryRun) {
        ... ...
         // 1) Find from scrap by position
            if (holder == null) {
                holder = getScrapViewForPosition(position, INVALID_TYPE, dryRun);
                if (holder != null) {
                    if (!validateViewHolderForOffsetPosition(holder)) {
                        // recycle this scrap
                        if (!dryRun) {
                            // we would like to recycle this but need to make sure it is not used by
                            // animation logic etc.
                            holder.addFlags(ViewHolder.FLAG_INVALID);
                            if (holder.isScrap()) {
                                removeDetachedView(holder.itemView, false);
                                holder.unScrap();
                            } else if (holder.wasReturnedFromScrap()) {
                                holder.clearReturnedFromScrapFlag();
                            }
                            recycleViewHolderInternal(holder);
                        }
                        holder = null;
                    } else {
                        fromScrap = true;
                    }
                }
            }
        ... ... 
        if (holder == null) { // fallback to recycler
                    // try recycler.
                    // Head to the shared pool.
                    if (DEBUG) {
                        Log.d(TAG, "getViewForPosition(" + position + ") fetching from shared "
                                + "pool");
                    }
                    holder = getRecycledViewPool().getRecycledView(type);
                    if (holder != null) {
                        holder.resetInternal();
                        if (FORCE_INVALIDATE_DISPLAY_LIST) {
                            invalidateDisplayListInt(holder);
                        }
                    }
        }
        ... ...
    }

getViewForPosition方法只在LinearLayoutManager.LayoutState.next,其他LayoutManager没有的方法中调用

    View next(RecyclerView.Recycler recycler) {
            if (mScrapList != null) {
                return nextViewFromScrapList();
            }
            final View view = recycler.getViewForPosition(mCurrentPosition);
            mCurrentPosition += mItemDirection;
            return view;
     }

LinearLayoutManager.java:

    void layoutChunk(RecyclerView.Recycler recycler, RecyclerView.State state,
            LayoutState layoutState, LayoutChunkResult result) {
        View view = layoutState.next(recycler);
        ... ...
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        if (layoutState.mScrapList == null) {
            if (mShouldReverseLayout == (layoutState.mLayoutDirection
                    == LayoutState.LAYOUT_START)) {
                addView(view);
            } else {
                addView(view, 0);
            }
        } else {
            if (mShouldReverseLayout == (layoutState.mLayoutDirection
                    == LayoutState.LAYOUT_START)) {
                addDisappearingView(view);
            } else {
                addDisappearingView(view, 0);
            }
        }
        measureChildWithMargins(view, 0, 0);
        ... ...
        layoutDecoratedWithMargins(view, left, top, right, bottom);
        ... ...
    }
    int fill(RecyclerView.Recycler recycler, LayoutState layoutState,
            RecyclerView.State state, boolean stopOnFocusable) {
            ... ...
            while ((layoutState.mInfinite || remainingSpace > 0) && layoutState.hasMore(state)) {
                layoutChunkResult.resetInternal();
                layoutChunk(recycler, state, layoutState, layoutChunkResult);
                if (layoutChunkResult.mFinished) {
                   break;
                }
                ... ...
           }
           ... ...
    }
    
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        // layout algorithm:
        // 1) by checking children and other variables, find an anchor coordinate and an anchor
        //  item position.
        // 2) fill towards start, stacking from bottom
        // 3) fill towards end, stacking from top
        // 4) scroll to fulfill requirements like stack from bottom.
        ... ...
    }

RecyclerView.java:

    private void dispatchLayoutStep1() {
        ... ...
        mLayout.onLayoutChildren(mRecycler, mState);
        ... ...
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70765.html

相关文章

  • 嗨!这是篇值得深入学习的控件-RecyclerView源码解析篇)

    摘要:其实通过父类的这个方法之后会调用它的方法,这个名字熟悉自定义的童鞋都知道了。 为什么要写这篇源码解析呢? 我一直在说RecyclerView是一个值得深入学习,甚至可以说是一门具有艺术性的控件。那到底哪里值得我们花时间去深入学习呢。没错了,就是源码的设计。但是看源码其实是一件不简单的事情,就拿RecyclerView的源码来说,打开源码一看,往下拉啊拉啊,我擦,怎么还没到头,汗.......

    myeveryheart 评论0 收藏0
  • RecyclerView问题汇总

    摘要:缺点自动装箱的存在意味着每一次插入都会有额外的对象创建。对象本身是一层额外需要被创建以及被垃圾回收的对象。相较于我们舍弃了和类型的放弃了并依赖于二分法查找。 目录介绍 25.0.0.0 请说一下RecyclerView?adapter的作用是什么,几个方法是做什么用的?如何理解adapter订阅者模式? 25.0.0.1 ViewHolder的作用是什么?如何理解ViewHolder...

    boredream 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<