Android RecyclerView 绘制流程及Recycler缓存,【好文推荐】

}

如果在 onMeasure 阶段没有执行 dispatchLayoutStep2() 方法去测量子 View,则会在 onLayout 阶段重新执行。

dispatchLayoutStep2

//在此步骤中,我们对最终状态的视图进行实际布局。

//如有必要,可多次运行此步骤(例如,measure)。

private void dispatchLayoutStep2() {

// Step 2: Run layout

mState.mInPreLayout = false;

mLayout.onLayoutChildren(mRecycler, mState);

}

public void onLayoutChildren(Recycler recycler, State state) {

Log.e(TAG, "You must override onLayoutChildren(Recycler recycler, State state) ");

}

核心逻辑是调用了 mLayout 的 onLayoutChildren 方法。

这个方法在 RecyclerView.LayoutManager 中的一个空方法,主要作用是测量 RecyclerView 内的子 View 大小,并确定它们所在的位置。

LinearLayoutManager、GridLayoutManager,以及 StaggeredLayoutManager 都分别复写了这个方法,并实现了不同方式的布局。

LinearLayoutManager.onLayoutChildren

@Override

public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {

//调用 fill 方法,完成子 View 的测量布局工作;

fill(recycler, mLayoutState, state, false);

}

//重点

int fill(RecyclerView.Recycler recycler, LayoutState layoutState,

RecyclerView.State state, boolean stopOnFocusable) {

while ((layoutState.mInfinite || remainingSpace > 0) && layoutState.hasMore(state)) {

layoutChunkResult.resetInternal();

if (RecyclerView.VERBOSE_TRACING) {

TraceCompat.beginSection(“LLM LayoutChunk”);

}

//子 View 测量布局的真正实现,每次执行完之后需要重新计算 remainingSpace。

layoutChunk(recycler, state, layoutState, layoutChunkResult);

if (!layoutChunkResult.mIgnoreConsumed || layoutState.mScrapList != null

|| !state.isPreLayout()) {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值