public class StaggeredGridLayoutManager
extends RecyclerView.LayoutManager
| java.lang.Object | ||
| android.support.v7.widget.RecyclerView.LayoutManager | ||
| android.support.v7.widget.StaggeredGridLayoutManager | ||
一个布局管理器,以交错的格子排列布局儿童。 它支持水平和垂直布局以及反向布局儿童的能力。
交错网格可能在布局的边缘存在间隙。 为了避免这些间隙,StaggeredGridLayoutManager可以独立偏移跨度或在跨度间移动项目。 您可以通过setGapStrategy(int)来控制此行为。
Nested classes |
|
|---|---|
class |
StaggeredGridLayoutManager.LayoutParams 由StaggeredGridLayoutManager使用的LayoutParams。 |
Inherited XML attributes |
|
|---|---|
android.support.v7.widget.RecyclerView.LayoutManager
|
|
常量(Constants) |
|
|---|---|
int |
GAP_HANDLING_LAZY 这个常数已被弃用。 不再支持。 |
int |
GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS 当滚动状态更改为 |
int |
GAP_HANDLING_NONE 没有做任何事情来隐藏差距。 |
int |
HORIZONTAL |
String |
TAG |
int |
VERTICAL |
Public constructors |
|
|---|---|
StaggeredGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) 当布局管理器由RecyclerView属性“layoutManager”设置为XML时使用的构造器。 |
|
StaggeredGridLayoutManager(int spanCount, int orientation) 用给定的参数创建一个StaggeredGridLayoutManager。 |
|
公共方法(Public methods) |
|
|---|---|
void |
assertNotInLayoutOrScroll(String message) 检查RecyclerView是否位于布局的中间或滚动,如果 是 ,则抛出 |
boolean |
canScrollHorizontally() 查询当前是否支持水平滚动。 |
boolean |
canScrollVertically() 查询当前是否支持垂直滚动。 |
boolean |
checkLayoutParams(RecyclerView.LayoutParams lp) 确定提供的LayoutParams对象的有效性。 |
int |
computeHorizontalScrollExtent(RecyclerView.State state) 如果您想支持滚动条,请覆盖此方法。 |
int |
computeHorizontalScrollOffset(RecyclerView.State state) 如果您想支持滚动条,请覆盖此方法。 |
int |
computeHorizontalScrollRange(RecyclerView.State state) 如果您想支持滚动条,请覆盖此方法。 |
int |
computeVerticalScrollExtent(RecyclerView.State state) 如果您想支持滚动条,请覆盖此方法。 |
int |
computeVerticalScrollOffset(RecyclerView.State state) 如果您想支持滚动条,请覆盖此方法。 |
int |
computeVerticalScrollRange(RecyclerView.State state) 如果您想支持滚动条,请覆盖此方法。 |
int[] |
findFirstCompletelyVisibleItemPositions(int[] into) 返回每个跨度的第一个完全可见视图的适配器位置。 |
int[] |
findFirstVisibleItemPositions(int[] into) 返回每个跨度的第一个可见视图的适配器位置。 |
int[] |
findLastCompletelyVisibleItemPositions(int[] into) 返回每个跨度最后一个完全可见视图的适配器位置。 |
int[] |
findLastVisibleItemPositions(int[] into) 返回每个跨度的最后一个可见视图的适配器位置。 |
RecyclerView.LayoutParams |
generateDefaultLayoutParams() 为RecyclerView的子项创建一个默认的 |
RecyclerView.LayoutParams |
generateLayoutParams(Context c, AttributeSet attrs) 从膨胀的布局资源中创建适合此LayoutManager的LayoutParams对象。 |
RecyclerView.LayoutParams |
generateLayoutParams(ViewGroup.LayoutParams lp) 如果可能,创建一个适用于此LayoutManager的LayoutParams对象,从所提供的LayoutParams对象中复制相关值。 |
int |
getColumnCountForAccessibility(RecyclerView.Recycler recycler, RecyclerView.State state) 返回可访问性的列数。 |
int |
getGapStrategy() 返回StaggeredGridLayoutManager的当前间隙处理策略。 |
int |
getOrientation() |
boolean |
getReverseLayout() 返回视图是否按相反顺序排列。 |
int |
getRowCountForAccessibility(RecyclerView.Recycler recycler, RecyclerView.State state) 返回可访问性的行数。 |
int |
getSpanCount() 返回StaggeredGridLayoutManager布置的跨度数。 |
void |
invalidateSpanAssignments() 为了一致性,StaggeredGridLayoutManager保持跨度和项目之间的映射。 |
void |
offsetChildrenHorizontal(int dx) 沿水平轴偏移附加到父RecyclerView的所有子视图dx个像素。 |
void |
offsetChildrenVertical(int dy) 沿垂直轴偏移由dy像素附加到父级RecyclerView的所有子视图。 |
void |
onDetachedFromWindow(RecyclerView view, RecyclerView.Recycler recycler) 当此LayoutManager与其父RecyclelerView分离时,或者其父RecyclerView从其窗口分离时调用。 |
View |
onFocusSearchFailed(View focused, int direction, RecyclerView.Recycler recycler, RecyclerView.State state) 当在给定方向上搜索可聚焦视图时调用RecyclerView的当前内容失败。 |
void |
onInitializeAccessibilityEvent(AccessibilityEvent event) |
void |
onInitializeAccessibilityNodeInfoForItem(RecyclerView.Recycler recycler, RecyclerView.State state, View host, AccessibilityNodeInfoCompat info) 当应该填充特定项目的可访问性信息时,由AccessibilityDelegate调用。 |
void |
onItemsAdded(RecyclerView recyclerView, int positionStart, int itemCount) 当项目已添加到适配器时调用。 |
void |
onItemsChanged(RecyclerView recyclerView) 在 |
void |
onItemsMoved(RecyclerView recyclerView, int from, int to, int itemCount) 在使用适配器移动项目时调用。 |
void |
onItemsRemoved(RecyclerView recyclerView, int positionStart, int itemCount) 当从适配器中移除项目时调用。 |
void |
onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount, Object payload) 当适配器和可选有效载荷中的项目已更改时调用。 |
void |
onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) 从给定的适配器中布置所有相关的子视图。 |
void |
onLayoutCompleted(RecyclerView.State state) 完整布局计算完成后调用。 |
void |
onRestoreInstanceState(Parcelable state) |
Parcelable |
onSaveInstanceState() 当LayoutManager保存其状态时调用。 |
void |
onScrollStateChanged(int state) RecyclerView调用此方法来通知LayoutManager滚动状态已更改。 |
int |
scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) 以屏幕坐标中的dx像素水平滚动并返回行进的距离。 |
void |
scrollToPosition(int position) 滚动到指定的适配器位置。 |
void |
scrollToPositionWithOffset(int position, int offset) 使用从布局开始的给定偏移量滚动到指定的适配器位置。 |
int |
scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) 在屏幕坐标中垂直滚动dy像素并返回行进的距离。 |
void |
setGapStrategy(int gapStrategy) 设置StaggeredGridLayoutManager的间隙处理策略。 |
void |
setMeasuredDimension(Rect childrenBounds, int wSpec, int hSpec) 设置儿童给定边界框的测量尺寸和传递到 |
void |
setOrientation(int orientation) 设置布局的方向。 |
void |
setReverseLayout(boolean reverseLayout) 设置LayoutManager是否应该从UI的末尾开始放置项目。 |
void |
setSpanCount(int spanCount) 设置布局的跨度数。 |
void |
smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) 平滑滚动到指定的适配器位置。 |
boolean |
supportsPredictiveItemAnimations() 返回此LayoutManager是否支持自动项目动画。 |
继承方法(Inherited methods) |
|
|---|---|
android.support.v7.widget.RecyclerView.LayoutManager
|
|
java.lang.Object
|
|
int GAP_HANDLING_LAZY
这个常数已被弃用。
不再支持。
常数值:1(0x00000001)
int GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
当滚动状态更改为SCROLL_STATE_IDLE ,StaggeredGrid将检查是否由于满量程项目而存在间隙。 如果发现,它将重新排版并使用动画将项目移动到正确的位置。
例如,如果布局管理器由于适配器更改而以下面的布局结束:
AAA _BC DDD
它将动画到以下状态:
AAA BC_ DDD
常量值:2(0x00000002)
int GAP_HANDLING_NONE
没有做任何事情来隐藏差距。
常量值:0(0x00000000)
int HORIZONTAL
常量值:0(0x00000000)
int VERTICAL
常数值:1(0x00000001)
StaggeredGridLayoutManager (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
当布局管理器由RecyclerView属性“layoutManager”设置为XML时使用的构造器。 默认为单列和垂直。
| 参数(Parameters) | |
|---|---|
context |
Context
|
attrs |
AttributeSet
|
defStyleAttr |
int
|
defStyleRes |
int
|
StaggeredGridLayoutManager (int spanCount,
int orientation)
用给定的参数创建一个StaggeredGridLayoutManager。
| 参数(Parameters) | |
|---|---|
spanCount |
int: If orientation is vertical, spanCount is number of columns. If orientation is horizontal, spanCount is number of rows. |
orientation |
int: VERTICAL or HORIZONTAL |
void assertNotInLayoutOrScroll (String message)
检查RecyclerView是否在布局中或滚动并且如果 是 ,则抛出 IllegalStateException 。
| 参数(Parameters) | |
|---|---|
message |
String: The message for the exception. Can be null. |
boolean canScrollHorizontally ()
查询当前是否支持水平滚动。 默认实现返回false。
| 返回(Returns) | |
|---|---|
boolean |
True if this LayoutManager can scroll the current contents horizontally |
boolean canScrollVertically ()
查询当前是否支持垂直滚动。 默认实现返回false。
| 返回(Returns) | |
|---|---|
boolean |
True if this LayoutManager can scroll the current contents vertically |
boolean checkLayoutParams (RecyclerView.LayoutParams lp)
确定提供的LayoutParams对象的有效性。
这应该检查以确保对象是正确的类型,并且所有值都在可接受的范围内。 对于非空参数,默认实现返回true 。
| 参数(Parameters) | |
|---|---|
lp |
RecyclerView.LayoutParams: LayoutParams object to check |
| 返回(Returns) | |
|---|---|
boolean |
true if this LayoutParams object is valid, false otherwise |
int computeHorizontalScrollExtent (RecyclerView.State state)
如果您想支持滚动条,请覆盖此方法。
详情请阅读 computeHorizontalScrollExtent() 。
默认实现返回0。
| 参数(Parameters) | |
|---|---|
state |
RecyclerView.State: Current state of RecyclerView |
| 返回(Returns) | |
|---|---|
int |
The horizontal extent of the scrollbar's thumb |
int computeHorizontalScrollOffset (RecyclerView.State state)
如果您想支持滚动条,请覆盖此方法。
详情请阅读 computeHorizontalScrollOffset() 。
默认实现返回0。
| 参数(Parameters) | |
|---|---|
state |
RecyclerView.State: Current State of RecyclerView where you can find total item count |
| 返回(Returns) | |
|---|---|
int |
The horizontal offset of the scrollbar's thumb |
int computeHorizontalScrollRange (RecyclerView.State state)
如果您想支持滚动条,请覆盖此方法。
详情请阅读 computeHorizontalScrollRange() 。
默认实现返回0。
| 参数(Parameters) | |
|---|---|
state |
RecyclerView.State: Current State of RecyclerView where you can find total item count |
| 返回(Returns) | |
|---|---|
int |
The total horizontal range represented by the vertical scrollbar |
int computeVerticalScrollExtent (RecyclerView.State state)
如果您想支持滚动条,请覆盖此方法。
详情请阅读 computeVerticalScrollExtent() 。
默认实现返回0。
| 参数(Parameters) | |
|---|---|
state |
RecyclerView.State: Current state of RecyclerView |
| 返回(Returns) | |
|---|---|
int |
The vertical extent of the scrollbar's thumb |
int computeVerticalScrollOffset (RecyclerView.State state)
如果您想支持滚动条,请覆盖此方法。
详情请阅读 computeVerticalScrollOffset() 。
默认实现返回0。
| 参数(Parameters) | |
|---|---|
state |
RecyclerView.State: Current State of RecyclerView where you can find total item count |
| 返回(Returns) | |
|---|---|
int |
The vertical offset of the scrollbar's thumb |
int computeVerticalScrollRange (RecyclerView.State state)
如果您想支持滚动条,请覆盖此方法。
阅读 computeVerticalScrollRange()了解详情。
默认实现返回0。
| 参数(Parameters) | |
|---|---|
state |
RecyclerView.State: Current State of RecyclerView where you can find total item count |
| 返回(Returns) | |
|---|---|
int |
The total vertical range represented by the vertical scrollbar |
int[] findFirstCompletelyVisibleItemPositions (int[] into)
返回每个跨度的第一个完全可见视图的适配器位置。
请注意,此值不受布局方向或项目顺序遍历的影响。 ( setReverseLayout(boolean) )。 视图按其在适配器中的位置排序,而不是在布局中排序。
如果RecyclerView具有项目装饰器,它们也将在计算中考虑。
StaggeredGridLayoutManager可以预先缓存一些不一定可见的视图。 这些视图在这种方法中被忽略。
| 参数(Parameters) | |
|---|---|
into |
int: An array to put the results into. If you don't provide any, LayoutManager will create a new one. |
| 返回(Returns) | |
|---|---|
int[] |
The adapter position of the first fully visible item in each span. If a span does not have any items, NO_POSITION is returned for that span. |
int[] findFirstVisibleItemPositions (int[] into)
返回每个跨度的第一个可见视图的适配器位置。
请注意,此值不受布局方向或项目顺序遍历的影响。 ( setReverseLayout(boolean) )。 视图按其在适配器中的位置排序,而不是在布局中排序。
如果RecyclerView具有项目装饰器,它们也将在计算中考虑。
StaggeredGridLayoutManager可以预先缓存一些不一定可见的视图。 这些视图在这种方法中被忽略。
| 参数(Parameters) | |
|---|---|
into |
int: An array to put the results into. If you don't provide any, LayoutManager will create a new one. |
| 返回(Returns) | |
|---|---|
int[] |
The adapter position of the first visible item in each span. If a span does not have any items, NO_POSITION is returned for that span. |
int[] findLastCompletelyVisibleItemPositions (int[] into)
返回每个跨度最后一个完全可见视图的适配器位置。
请注意,此值不受布局方向或项目顺序遍历的影响。 ( setReverseLayout(boolean) )。 视图按其在适配器中的位置排序,而不是在布局中排序。
如果RecyclerView具有项目装饰器,它们也将在计算中考虑。
StaggeredGridLayoutManager可以预先缓存一些不一定可见的视图。 这些视图在这种方法中被忽略。
| 参数(Parameters) | |
|---|---|
into |
int: An array to put the results into. If you don't provide any, LayoutManager will create a new one. |
| 返回(Returns) | |
|---|---|
int[] |
The adapter position of the last fully visible item in each span. If a span does not have any items, NO_POSITION is returned for that span. |
int[] findLastVisibleItemPositions (int[] into)
返回每个跨度的最后一个可见视图的适配器位置。
请注意,此值不受布局方向或项目顺序遍历的影响。 ( setReverseLayout(boolean) )。 视图按其在适配器中的位置排序,而不是在布局中排序。
如果RecyclerView具有项目装饰器,它们也将在计算中考虑。
StaggeredGridLayoutManager可以预先缓存一些不一定可见的视图。 这些视图在这种方法中被忽略。
| 参数(Parameters) | |
|---|---|
into |
int: An array to put the results into. If you don't provide any, LayoutManager will create a new one. |
| 返回(Returns) | |
|---|---|
int[] |
The adapter position of the last visible item in each span. If a span does not have any items, NO_POSITION is returned for that span. |
RecyclerView.LayoutParams generateDefaultLayoutParams ()
为RecyclerView的子项创建一个默认的 LayoutParams对象。
LayoutManagers通常会使用自定义的LayoutParams类型来存储特定于布局的额外信息。 客户端代码应为此目的RecyclerView.LayoutParams 。
重要提示:如果您使用自己定制的 LayoutParams类型,你还必须重写 checkLayoutParams(LayoutParams) , generateLayoutParams(android.view.ViewGroup.LayoutParams)和 generateLayoutParams(android.content.Context, android.util.AttributeSet) 。
| 返回(Returns) | |
|---|---|
RecyclerView.LayoutParams |
A new LayoutParams for a child view |
RecyclerView.LayoutParams generateLayoutParams (Context c, AttributeSet attrs)
从膨胀的布局资源中创建适合此LayoutManager的LayoutParams对象。
重要提示:如果您使用自己定制的 LayoutParams类型,你还必须重写 checkLayoutParams(LayoutParams) , generateLayoutParams(android.view.ViewGroup.LayoutParams)和 generateLayoutParams(android.content.Context, android.util.AttributeSet) 。
| 参数(Parameters) | |
|---|---|
c |
Context: Context for obtaining styled attributes |
attrs |
AttributeSet: AttributeSet describing the supplied arguments |
| 返回(Returns) | |
|---|---|
RecyclerView.LayoutParams |
a new LayoutParams object |
RecyclerView.LayoutParams generateLayoutParams (ViewGroup.LayoutParams lp)
如果可能,创建一个适用于此LayoutManager的LayoutParams对象,从所提供的LayoutParams对象中复制相关值。
重要提示:如果您使用自己定制的 LayoutParams类型,你还必须重写 checkLayoutParams(LayoutParams) , generateLayoutParams(android.view.ViewGroup.LayoutParams)和 generateLayoutParams(android.content.Context, android.util.AttributeSet) 。
| 参数(Parameters) | |
|---|---|
lp |
ViewGroup.LayoutParams: Source LayoutParams object to copy values from |
| 返回(Returns) | |
|---|---|
RecyclerView.LayoutParams |
a new LayoutParams object |
int getColumnCountForAccessibility (RecyclerView.Recycler recycler, RecyclerView.State state)
返回可访问性的列数。
如果LayoutManager支持水平滚动,则默认实现返回适配器中的项目数,如果LayoutManager不支持水平滚动,则返回1。
| 参数(Parameters) | |
|---|---|
recycler |
RecyclerView.Recycler: The Recycler that can be used to convert view positions into adapter positions |
state |
RecyclerView.State: The current state of RecyclerView |
| 返回(Returns) | |
|---|---|
int |
The number of rows in LayoutManager for accessibility. |
int getGapStrategy ()
返回StaggeredGridLayoutManager的当前间隙处理策略。
由于适配器的变化,交错网格可能在布局上存在间隙。 为了避免差距,StaggeredGridLayoutManager提供了2个选项。 详情请查询GAP_HANDLING_NONE和GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS 。
默认情况下,StaggeredGridLayoutManager使用 GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS 。
| 返回(Returns) | |
|---|---|
int |
Current gap handling strategy. |
int getOrientation ()
| 返回(Returns) | |
|---|---|
int |
|
boolean getReverseLayout ()
返回视图是否按相反顺序排列。
不是说这个值不受RecyclerView布局方向的影响。
| 返回(Returns) | |
|---|---|
boolean |
True if layout is reversed, false otherwise |
也可以看看:
int getRowCountForAccessibility (RecyclerView.Recycler recycler, RecyclerView.State state)
返回可访问性的行数。
如果LayoutManager支持垂直滚动,则默认实现返回适配器中的项目数,如果LayoutManager不支持垂直滚动,则返回1。
| 参数(Parameters) | |
|---|---|
recycler |
RecyclerView.Recycler: The Recycler that can be used to convert view positions into adapter positions |
state |
RecyclerView.State: The current state of RecyclerView |
| 返回(Returns) | |
|---|---|
int |
The number of rows in LayoutManager for accessibility. |
int getSpanCount ()
返回StaggeredGridLayoutManager布置的跨度数。
| 返回(Returns) | |
|---|---|
int |
Number of spans in the layout |
void invalidateSpanAssignments ()
为了一致性,StaggeredGridLayoutManager保持跨度和项目之间的映射。
如果您需要取消当前分配,可以调用此方法清除所有分配并请求新布局。
void offsetChildrenHorizontal (int dx)
沿水平轴偏移附加到父RecyclerView的所有子视图dx个像素。
| 参数(Parameters) | |
|---|---|
dx |
int: Pixels to offset by |
void offsetChildrenVertical (int dy)
沿垂直轴偏移由dy像素附加到父级RecyclerView的所有子视图。
| 参数(Parameters) | |
|---|---|
dy |
int: Pixels to offset by |
void onDetachedFromWindow (RecyclerView view, RecyclerView.Recycler recycler)
当此LayoutManager与其父RecyclelerView分离时,或者其父RecyclerView从其窗口分离时调用。
LayoutManager应清除所有视图引用,因为另一个LayoutManager可能被分配给RecyclerView。
如果RecyclerView重新连接了相同的LayoutManager和Adapter,那么它可能不会调用 onLayoutChildren(Recycler, State)如果没有更改,并且RecyclerView在分离时没有请求布局。
如果您的LayoutManager具有在分离时清理的View引用,它还应该调用 requestLayout()以确保在重新连接RecyclerView时重新布局该 requestLayout() 。
子类实现应该始终调用超类的实现。
| 参数(Parameters) | |
|---|---|
view |
RecyclerView: The RecyclerView this LayoutManager is bound to |
recycler |
RecyclerView.Recycler: The recycler to use if you prefer to recycle your children instead of keeping them around. |
View onFocusSearchFailed (View focused, int direction, RecyclerView.Recycler recycler, RecyclerView.State state)
当在给定方向上搜索可聚焦视图时调用RecyclerView的当前内容失败。
这是LayoutManager在给定方向上填充视图以满足请求的机会(如果可以)。 LayoutManager应该附加并返回要聚焦的视图。 默认实现返回null。
| 参数(Parameters) | |
|---|---|
focused |
View: The currently focused view |
direction |
int: One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_BACKWARD, FOCUS_FORWARD or 0 for not applicable |
recycler |
RecyclerView.Recycler: The recycler to use for obtaining views for currently offscreen items |
state |
RecyclerView.State: Transient state of RecyclerView |
| 返回(Returns) | |
|---|---|
View |
The chosen view to be focused |
void onInitializeAccessibilityEvent (AccessibilityEvent event)
| 参数(Parameters) | |
|---|---|
event |
AccessibilityEvent
|
void onInitializeAccessibilityNodeInfoForItem (RecyclerView.Recycler recycler, RecyclerView.State state, View host, AccessibilityNodeInfoCompat info)
当应该填充特定项目的可访问性信息时,由AccessibilityDelegate调用。
默认实现添加有关该项目的基本定位信息。
| 参数(Parameters) | |
|---|---|
recycler |
RecyclerView.Recycler: The Recycler that can be used to convert view positions into adapter positions |
state |
RecyclerView.State: The current state of RecyclerView |
host |
View: The child for which accessibility node info should be populated |
info |
AccessibilityNodeInfoCompat: The info to fill out about the item |
void onItemsAdded (RecyclerView recyclerView, int positionStart, int itemCount)
当项目已添加到适配器时调用。 如果插入的项目需要刷新当前可见的一组子视图,则LayoutManager可以选择requestLayout。 (例如,当前空白空间将由附加项目填充等)
void onItemsChanged (RecyclerView recyclerView)
在 notifyDataSetChanged()被触发时调用,而不是提供实际更改内容的详细信息。
void onItemsMoved (RecyclerView recyclerView, int from, int to, int itemCount)
在使用适配器移动项目时调用。
请注意,一个项目也可能会改变位置以响应另一个ADD / REMOVE / MOVE操作。 此回调仅在调用notifyItemMoved(int, int)调用。
void onItemsRemoved (RecyclerView recyclerView, int positionStart, int itemCount)
当从适配器中移除项目时调用。
void onItemsUpdated (RecyclerView recyclerView, int positionStart, int itemCount, Object payload)
当适配器和可选有效载荷中的项目已更改时调用。 默认实现调用onItemsUpdated(RecyclerView, int, int) 。
void onLayoutChildren (RecyclerView.Recycler recycler, RecyclerView.State state)
从给定的适配器中布置所有相关的子视图。 LayoutManager负责项目动画的行为。 默认情况下,RecyclerView具有非空ItemAnimator ,并且启用简单的项目动画。 这意味着对适配器的添加/删除操作将导致动画添加新的或出现的项目,删除或消失的项目以及移动的项目。 如果LayoutManager从supportsPredictiveItemAnimations() (这是默认设置)返回false并在onLayoutChildren(Recycler, State)期间运行正常布局操作,那么RecyclerView将具有足够的信息以简单的方式运行这些动画。 例如,默认的ItemAnimator, DefaultItemAnimator ,只是淡入淡出视图,无论它们是实际添加/删除,还是由于其他添加/删除操作而在屏幕上移动或移出。
LayoutManager需要更好的项目动画体验,其中项目可以根据项目在不在屏幕上时存在的位置在屏幕上和屏幕之外动画,然后LayoutManager应该从supportsPredictiveItemAnimations()返回true并将其他逻辑添加到onLayoutChildren(Recycler, State) 。 支持预测动画意味着onLayoutChildren(Recycler, State)将被调用两次; 一次是作为“预先”布局步骤,以确定项目在真实布局之前的位置,并再次执行“真实”布局。 在预先布置阶段,项目会记住他们的布局前位置,以便适当地布置它们。 此外,将从废料中返回removed件物品,以帮助确定其他物品的正确放置位置。 这些删除的项目不应该添加到子列表中,而应该用于帮助计算其他视图的正确定位,包括以前不在屏幕上的视图(称为APPEARING视图),但可以确定其前置布局离屏位置给出有关预布局移除视图的额外信息。
第二个布局过程是真正的布局,其中只使用未删除的视图。 在此过程中唯一的附加要求是,如果supportsPredictiveItemAnimations()返回true,请注意在布局之前哪些视图存在于子列表中,哪些不在布局之后(称为DISAPPEARING视图),并适当地定位/布置这些视图,而不考虑RecyclerView的实际范围。 这允许动画系统知道动画这些消失的视图的位置。
RecyclerView的默认LayoutManager实现已经处理了所有动画需求。 RecyclerView的客户可以直接使用这些布局管理器中的一个,或者查看onLayoutChildren()的实现来了解它们如何解释APPEARING和DISAPPEARING视图。
| 参数(Parameters) | |
|---|---|
recycler |
RecyclerView.Recycler: Recycler to use for fetching potentially cached views for a position |
state |
RecyclerView.State: Transient state of RecyclerView |
void onLayoutCompleted (RecyclerView.State state)
完整布局计算完成后调用。 由于动画或布局测量,布局计算可能包含多个onLayoutChildren(Recycler, State)通话,但它只包含一个onLayoutCompleted(State)通话。 该方法将在layout(int, int, int, int)调用结束时调用。
这是LayoutManager做一些清理的好地方,例如待处理的滚动位置,保存的状态等。
| 参数(Parameters) | |
|---|---|
state |
RecyclerView.State: Transient state of RecyclerView |
void onRestoreInstanceState (Parcelable state)
| 参数(Parameters) | |
|---|---|
state |
Parcelable
|
Parcelable onSaveInstanceState ()
当LayoutManager保存其状态时调用。 如果重新创建LayoutManager,这是保存滚动位置,配置和恢复相同布局状态所需的其他任何东西的好时机。
RecyclerView不会验证LayoutManager是否在状态保存和恢复之间更改。 这可以让你在你的布局管理器之间共享信息,但它也是你的责任,确保他们使用相同的可分类。
| 返回(Returns) | |
|---|---|
Parcelable |
Necessary information for LayoutManager to be able to restore its state |
void onScrollStateChanged (int state)
RecyclerView调用此方法来通知LayoutManager滚动状态已更改。
| 参数(Parameters) | |
|---|---|
state |
int: The new scroll state for RecyclerView |
int scrollHorizontallyBy (int dx,
RecyclerView.Recycler recycler,
RecyclerView.State state)
以屏幕坐标中的dx像素水平滚动并返回行进的距离。 默认实现不做任何事情并返回0。
| 参数(Parameters) | |
|---|---|
dx |
int: distance to scroll by in pixels. X increases as scroll position approaches the right. |
recycler |
RecyclerView.Recycler: Recycler to use for fetching potentially cached views for a position |
state |
RecyclerView.State: Transient state of RecyclerView |
| 返回(Returns) | |
|---|---|
int |
The actual distance scrolled. The return value will be negative if dx was negative and scrolling proceeeded in that direction. Math.abs(result) may be less than dx if a boundary was reached. |
void scrollToPosition (int position)
滚动到指定的适配器位置。 该项目在屏幕上的实际位置取决于LayoutManager实施。
| 参数(Parameters) | |
|---|---|
position |
int: Scroll to this adapter position. |
void scrollToPositionWithOffset (int position,
int offset)
使用从布局开始的给定偏移量滚动到指定的适配器位置。
请注意,直到下一次布局调用时才会反映滚动位置更改。
如果您只是想让位置可见,请使用 scrollToPosition(int) 。
| 参数(Parameters) | |
|---|---|
position |
int: Index (starting at 0) of the reference item. |
offset |
int: The distance (in pixels) between the start edge of the item view and start edge of the RecyclerView. |
int scrollVerticallyBy (int dy,
RecyclerView.Recycler recycler,
RecyclerView.State state)
在屏幕坐标中垂直滚动dy像素并返回行进的距离。 默认实现不做任何事情并返回0。
| 参数(Parameters) | |
|---|---|
dy |
int: distance to scroll in pixels. Y increases as scroll position approaches the bottom. |
recycler |
RecyclerView.Recycler: Recycler to use for fetching potentially cached views for a position |
state |
RecyclerView.State: Transient state of RecyclerView |
| 返回(Returns) | |
|---|---|
int |
The actual distance scrolled. The return value will be negative if dy was negative and scrolling proceeeded in that direction. Math.abs(result) may be less than dy if a boundary was reached. |
void setGapStrategy (int gapStrategy)
设置StaggeredGridLayoutManager的间隙处理策略。 如果gapStrategy参数与当前策略不同,则调用此方法将触发布局请求。
| 参数(Parameters) | |
|---|---|
gapStrategy |
int: The new gap handling strategy. Should be GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS or GAP_HANDLING_NONE. |
也可以看看:
void setMeasuredDimension (Rect childrenBounds, int wSpec, int hSpec)
设置儿童给定边界框的测量尺寸和传递到onMeasure(int, int)的测量规格。 它是在RecyclerView在测量过程中调用onLayoutChildren(Recycler, State)之后调用的。
该方法应该调用 setMeasuredDimension(int, int) 。
默认实现将RecyclerView的填充添加到给定的边界框,然后将该值限制在给定的测量规格内。
仅当LayoutManager选择进入自动测量API时才调用此方法。
| 参数(Parameters) | |
|---|---|
childrenBounds |
Rect: The bounding box of all children |
wSpec |
int: The widthMeasureSpec that was passed into the RecyclerView. |
hSpec |
int: The heightMeasureSpec that was passed into the RecyclerView. |
void setOrientation (int orientation)
设置布局的方向。 如果在布置视图后调用此方法,StaggeredGridLayoutManager将尽最大努力保持滚动位置。
| 参数(Parameters) | |
|---|---|
orientation |
int: HORIZONTAL or VERTICAL |
void setReverseLayout (boolean reverseLayout)
设置LayoutManager是否应该从UI的末尾开始放置项目。 遍历的订单项不受此调用的影响。
对于垂直布局,如果它设置为 true ,则第一项将位于列表的底部。
对于水平布局,它取决于布局方向。 当设置为true时,如果RecyclerView是LTR,则它将从RTL布局,如果RecyclerView }是RTL,则它将从LTR进行布局。
| 参数(Parameters) | |
|---|---|
reverseLayout |
boolean: Whether layout should be in reverse or not |
void setSpanCount (int spanCount)
设置布局的跨度数。 这将使视图的所有跨度分配无效。
调用此方法将自动生成新的布局请求,除非spanCount参数等于当前的跨度计数。
| 参数(Parameters) | |
|---|---|
spanCount |
int: Number of spans to layout |
void smoothScrollToPosition (RecyclerView recyclerView, RecyclerView.State state, int position)
平滑滚动到指定的适配器位置。
要支持平滑滚动,请覆盖此方法,创建 RecyclerView.SmoothScroller实例并调用 startSmoothScroll(SmoothScroller) 。
| 参数(Parameters) | |
|---|---|
recyclerView |
RecyclerView: The RecyclerView to which this layout manager is attached |
state |
RecyclerView.State: Current State of RecyclerView |
position |
int: Scroll to this adapter position. |
boolean supportsPredictiveItemAnimations ()
返回此LayoutManager是否支持自动项目动画。 希望支持项目动画的LayoutManager应遵循onLayoutChildren(Recycler, State)列出的某些规则。 默认返回值为false ,因此LayoutManager的子类默认情况下不会获得预测性项目动画。
在RecyclerView中是否启用项目动画由来自此方法的返回值和RecyclerView自身上设置的ItemAnimator决定。 如果RecyclerView有一个非零的ItemAnimator,但此方法返回false,则将启用简单的项目动画,其中移动或移出屏幕的视图只是淡入/淡出。 如果RecyclerView有一个非null的ItemAnimator并且这个方法返回true,那么将会有两次调用onLayoutChildren(Recycler, State)来建立所需的信息,以更智能地预测出现和消失的视图应该从/到哪里动画。
| 返回(Returns) | |
|---|---|
boolean |
true if predictive item animations should be enabled, false otherwise |