private void dispatchLayoutStep1() {
…
if (mState.mRunPredictiveAnimations) {
…
mLayout.onLayoutChildren(mRecycler, mState);
…
} else {
clearOldPositions();
}
…
}
dispatchLayoutStep1
方法中调用了 mLayout
的 onLayoutChildren
方法。上面分析告诉我们,mLayout
就是 LayoutManager
,所以我们转到 LayoutManager
的 onLayoutChildren
方法。
public void onLayoutChildren(Recycler recycler, State state) {
Log.e(TAG, "You must override onLayoutChildren(Recycler recycler, State state) ");
}
onLayoutChildren
方法是一个空实现,其具体实现在各个子类中。我们拿 LinearLayoutManager
进行分析,看其中 onLayoutChildren
的实现。
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
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
// 4) scroll to fulfill requirements like stack from bottom.
// create layout state
…
if (mAnchorInfo.mLayoutFromEnd) {
…
} else {
// fill towards end
updateLayoutStateToFillEnd(mAnchorInfo);
mLayoutState.mExtra = extraForEnd;
fill(recycler, mLayoutState, state, false);
endOffset = mLayoutState.mOffset;
final int lastElement = mLayoutState.mCurrentPosition;
if (mLayoutState.mAvailable > 0) {
extraForStart += mLayoutState.mAvailable;
}
// fill towards start
updateLayoutStateToFillStart(mAnchorInfo);
mLayoutState.mExtra = extraForStart;