在使用RecyclerView时,关于GridLayoutManager如何绘制布局的理解,我用的是android25.1.1中的API源码。
这里是一个大神关于RecyclerView绘制流程的详解 http://blog.csdn.net/hfyd_/article/details/53910631
背景:
以下讲解,是在RecyclerView设置了GridLayoutManager的情况下,方向为vertical
流程onMeasure——onLayout——ondraw(fill)
这里只讨论onlayoutchild(),从上边的文章中,我得知mLayout.onLayoutChildren(mRecycler, mState);这里的mlayout说是我们通过setLayoutManager()传入的,那我们就看一下具体是如何布局的。
GridLayoutManager中实现了这个方法:
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { if (state.isPreLayout()) { cachePreLayoutSpanMapping(); } super.onLayoutChildren(recycler, state); if (DEBUG) { validateChildOrder(); } clearPreLayoutSpanMappingCache(); }
我们看到调用了super.onlayotChildren();所以继续进入查看(继承了LinearLayoutManager)
在LinearLayoutManager中,这个方法调用了fill();所以我们继续到fill()中查看:
int fill(RecyclerView.Recycler recycler, LayoutState layoutState, RecyclerView.State state, boolean stopOnFocusable) { // max offset we should set is mFastScroll + available final int start = layoutState.mAvailable; if (layoutState.mScrollingOffset != LayoutState.SCROLLING_OFFSET_NaN) { // TODO ugly bug fix. should not happen if (layoutState.mAvailable < 0) { layoutState.mScrollingOffset += layoutState.mAvailable; } recycleByLayoutState(recycler, layoutState); } int remainingSpace = layoutState.mAvailable + layoutState.mExtra; LayoutChunkResult layoutChunkResult = mLayoutChunkResult; while ((layoutState.mInfinite || remainingSpace > 0) && layoutState.hasMore(state)) { layoutChunkResult.resetInternal(); layoutChunk(recycler, state, layoutState, layoutChunkResult); if (layoutChunkResult.mFinished) { break; }
在while中调用了layoutChunk(),对子view进行测量,布局,绘制。所以这就是我要重点说的部分。在while中判断remainingSpace 是否大于0,我认为这是判断子view还需不需要
继续填充,而layoutChunk()是对RecyclerView中的一行进行绘制(假设我们设置的方向是ve