Mais conteúdo relacionado Semelhante a Deep Dive Into LayoutManager for RecyclerView (20) Deep Dive Into LayoutManager for RecyclerView13. Think before you build it
Much harder than custom view
Gives you much flexibility
on top of RecyclerView features
25. private int fill(RecyclerView.Recycler recycler,
RecyclerView.State state,
LayoutState layoutState) {
int start = layoutState.mAvailable;
int remainingSpace = layoutState.mAvailable;
int consumed = 0;
while (remainingSpace > 0 &&
layoutState.hasMore(state, mFlexLines)) {
FlexLine flexLine =
mFlexLines.get(layoutState.mFlexLinePosition);
layoutState.mPosition = flexLine.mFirstIndex;
consumed += layoutFlexLine(flexLine, layoutState);
remainingSpace -= flexLine.getCrossSize();
}
layoutState.mAvailable -= consumed;
return start - layoutState.mAvailable;
}
Simplified version of fill
26. private int fill(RecyclerView.Recycler recycler,
RecyclerView.State state,
LayoutState layoutState) {
int start = layoutState.mAvailable;
int remainingSpace = layoutState.mAvailable;
int consumed = 0;
while (remainingSpace > 0 &&
layoutState.hasMore(state, mFlexLines)) {
FlexLine flexLine =
mFlexLines.get(layoutState.mFlexLinePosition);
layoutState.mPosition = flexLine.mFirstIndex;
consumed += layoutFlexLine(flexLine, layoutState);
remainingSpace -= flexLine.getCrossSize();
}
layoutState.mAvailable -= consumed;
return start - layoutState.mAvailable;
}
mAvailable: Amount of pixels need to be filled
mDirection: START | END
27. private int fill(RecyclerView.Recycler recycler,
RecyclerView.State state,
LayoutState layoutState) {
int start = layoutState.mAvailable;
int remainingSpace = layoutState.mAvailable;
int consumed = 0;
while (remainingSpace > 0 &&
layoutState.hasMore(state, mFlexLines)) {
FlexLine flexLine =
mFlexLines.get(layoutState.mFlexLinePosition);
layoutState.mPosition = flexLine.mFirstIndex;
consumed += layoutFlexLine(flexLine, layoutState);
remainingSpace -= flexLine.getCrossSize();
}
layoutState.mAvailable -= consumed;
return start - layoutState.mAvailable;
}
Put views and
loop until mAvailable < 0
28. onLayoutChildren
1. Find the anchor position
2. Calculate how many items
should be in visible portion
3. Fill toward end
4. Fill toward start
31. 1 2
3
4 5
6 7
8 9
10
11
scrollToPosition(50)
42 43
44
45
46 47
48
49 50
Anchor position
34. @Override
public int scrollVerticallyBy(int dy,
RecyclerView.Recycler recycler,
RecyclerView.State state) {
if (isMainAxisDirectionHorizontal()) {
int scrolled =
handleScrollingCrossAxis(dy, recycler, state);
mViewCache.clear();
return scrolled;
} else {
int scrolled = handleScrollingMainAxis(dy);
mAnchorInfo.mPerpendicularCoordinate += scrolled;
mSubOrientationHelper.offsetChildren(-scrolled);
return scrolled;
}
}
35. @Override
public int scrollVerticallyBy(int dy,
RecyclerView.Recycler recycler,
RecyclerView.State state) {
if (isMainAxisDirectionHorizontal()) {
int scrolled =
handleScrollingCrossAxis(dy, recycler, state);
mViewCache.clear();
return scrolled;
} else {
int scrolled = handleScrollingMainAxis(dy);
mAnchorInfo.mPerpendicularCoordinate += scrolled;
mSubOrientationHelper.offsetChildren(-scrolled);
return scrolled;
}
}
Distance to scroll in pixels
36. @Override
public int scrollVerticallyBy(int dy,
RecyclerView.Recycler recycler,
RecyclerView.State state) {
if (isMainAxisDirectionHorizontal()) {
int scrolled =
handleScrollingCrossAxis(dy, recycler, state);
mViewCache.clear();
return scrolled;
} else {
int scrolled = handleScrollingMainAxis(dy);
mAnchorInfo.mPerpendicularCoordinate += scrolled;
mSubOrientationHelper.offsetChildren(-scrolled);
return scrolled;
}
}
The actual distance scrolled
52. Case1. Found in scrap
Case2. Found in Recycled Pool
Case3. Not found in cache
58. Use scrap if you re-attach in a
same layout pass
Use recycle for views no longer
needed
69. In pre-layout
Lay out all the views currently visible and
additional views that are appearing (or
disappearing) after the animation
74. Now you know the basics of
building a custom LayoutManager
75. Resources
Source of default LayoutManagers
github.com/google/flexbox-layout
wiresareobsolete.com/2014/09/building-a-recyclerview-
layoutmanager-part-1/