RecyclerView的layoutManager
Layoutmanager负责对recyclerview中的item, measure和layout, 也决定着回收那些不可见的item。⽀持list,grids和staggered grids的集合,本⽂主要分析下Linear和Grid的measure和layout过程。
Linea rLa yo utMa na ger-o nLa yo utChildren
r La
如果supportsPredictiveItemAnimations返回true, onLayoutchildren将会被执⾏2次。第⼀次pre-layout允许记录item的位置,即使这些item被移除,从scrap返回也会被重新放置,帮助计算其他的item位置。第⼆次是real-layout, 这次仅仅non-removed的views会被使⽤。如果⼀个view在pre-layout,但不在real-layout阶段,就会执⾏DISAPPEARING。看下LinearLayoutManager的具体实现。
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 fro怎么解除微信绑定的银行卡
m bottom.
// create layout state
ensureLayoutState();
mLayoutState.mRecycle = fal;
// resolve layout direction
resolveShouldLayoutRever();
final View focud李玮峰
= getFocudChild();
if (!mAnchorInfo.mValid || mPendingScrollPosition != RecyclerView.NO_POSITION
|| mPendingSavedState != null) {
<();
mAnchorInfo.mLayoutFromEnd = mShouldReverLayout ^ mStackFromEnd;
// calculate anchor position and coordinate
updateAnchorInfoFor成长的经历作文
Layout(recycler, state, mAnchorInfo);
mAnchorInfo.mValid = true;
} el if (focud != null && (DecoratedStart(focud)
>= EndAfterPadding()
|| m今后努力的方向
DecoratedEnd(focud)
<= m理论学习计划
StartAfterPadding())) {
mAnchorInfo.assignFromViewAndKeepVisibleRect(focud, getPosition(focud));
}
/
/ LLM may decide to layout items for "extra" pixels to account for scrolling target,
// caching or predictive animations.
mLayoutState.mLayoutDirection = mLayoutState.mLastScrollDelta >= 0
LinearLayoutManager.LayoutState.LAYOUT_END : LinearLayoutManager.LayoutState.LAYOUT_START;
mReusableIntPair[0] = 0;
mReusableIntPair[1] = 0;
calculateExtraLayoutSpace(state, mReusableIntPair);
int extraForStart = Math.max(0, mReusableIntPair[0])
+ StartAfterPadding();
int extraForEnd = Math.max(0, mReusableIntPair[1])
+ EndPadding();
if (state.isPreLayout() && mPendingScrollPosition != RecyclerView.NO_POSI最好卖最暴利的地摊货
TION
&& mPendingScrollPositionOfft != INVALID_OFFSET) {
// if the child is visible and we are going to move it around, we should layout
// extra items in the opposite direction to make sure new items animate nicely
// instead of just fading in
final View existing = findViewByPosition(mPendingScrollPosition);
if (existing != null) {
final int current;
final int upcomingOfft;
if (mShouldReverLayout) {
current = EndAfterPadding()
-
DecoratedEnd(existing);
upcomingOfft = current - mPendingScrollPositionOfft;
} el {
current = DecoratedStart(existing)
- StartAfterPadding();
upcomingOfft = mPendingS手抄报安全教育
crollPositionOfft - current;
}