LayoutManager

abstract class LayoutManager

A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user. By changing the LayoutManager a RecyclerView can be used to implement a standard vertically scrolling list, a uniform grid, staggered grids, horizontally scrolling collections and more. Several stock layout managers are provided for general use.

If the LayoutManager specifies a default constructor or one with the signature (Context, AttributeSet, int, int), RecyclerView will instantiate and set the LayoutManager when being inflated. Most used properties can be then obtained from getProperties. In case a LayoutManager specifies both constructors, the non-default constructor will take precedence.

Inheritors

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
Interface for LayoutManagers to request items to be prefetched, based on position, with specified distance from viewport, which indicates priority.
Link copied to clipboard
open class Properties
Some general properties that a LayoutManager may want to use.

Functions

Link copied to clipboard
open fun addDisappearingView(child: View)
open fun addDisappearingView(child: View, index: Int)
To be called only during onLayoutChildren to add a view to the layout that is known to be going away, either because it has been removed or because it is actually not in the visible portion of the container but is being laid out in order to inform RecyclerView in how to animate the item out of view.
Link copied to clipboard
open fun addView(child: View)
open fun addView(child: View, index: Int)
Add a view to the currently attached RecyclerView if needed.
Link copied to clipboard
open fun assertInLayoutOrScroll(message: String)
Checks if RecyclerView is in the middle of a layout or scroll and throws an IllegalStateException if it is not.
Link copied to clipboard
Checks if RecyclerView is in the middle of a layout or scroll and throws an IllegalStateException if it is.
Link copied to clipboard
open fun attachView(@NonNull child: @NonNull View)
open fun attachView(@NonNull child: @NonNull View, index: Int)
open fun attachView(@NonNull child: @NonNull View, index: Int, lp: RecyclerView.LayoutParams)
Reattach a previously detached view.
Link copied to clipboard
open fun calculateItemDecorationsForChild(@NonNull child: @NonNull View, @NonNull outRect: @NonNull Rect)
Calculates the item decor insets applied to the given child and updates the provided Rect instance with the inset values.
Link copied to clipboard
Query if horizontal scrolling is currently supported.
Link copied to clipboard
Query if vertical scrolling is currently supported.
Link copied to clipboard
Determines the validity of the supplied LayoutParams object.
Link copied to clipboard
open fun chooseSize(spec: Int, desired: Int, min: Int): Int
Chooses a size from the given specs and parameters that is closest to the desired size and also complies with the spec.
Link copied to clipboard
Gather all positions from the LayoutManager to be prefetched, given specified momentum.
Link copied to clipboard
Gather all positions from the LayoutManager to be prefetched in preperation for its RecyclerView to come on screen, due to the movement of another, containing RecyclerView.
Link copied to clipboard
open fun computeHorizontalScrollExtent(@NonNull state: @NonNull RecyclerView.State): Int
Override this method if you want to support scroll bars.
Link copied to clipboard
open fun computeHorizontalScrollOffset(@NonNull state: @NonNull RecyclerView.State): Int
Override this method if you want to support scroll bars.
Link copied to clipboard
open fun computeHorizontalScrollRange(@NonNull state: @NonNull RecyclerView.State): Int
Override this method if you want to support scroll bars.
Link copied to clipboard
open fun computeVerticalScrollExtent(@NonNull state: @NonNull RecyclerView.State): Int
Override this method if you want to support scroll bars.
Link copied to clipboard
open fun computeVerticalScrollOffset(@NonNull state: @NonNull RecyclerView.State): Int
Override this method if you want to support scroll bars.
Link copied to clipboard
open fun computeVerticalScrollRange(@NonNull state: @NonNull RecyclerView.State): Int
Override this method if you want to support scroll bars.
Link copied to clipboard
open fun detachAndScrapAttachedViews(@NonNull recycler: @NonNull RecyclerView.Recycler)
Temporarily detach and scrap all currently attached child views.
Link copied to clipboard
open fun detachAndScrapView(@NonNull child: @NonNull View, @NonNull recycler: @NonNull RecyclerView.Recycler)
Detach a child view and add it to a Recycler's scrap heap.
Link copied to clipboard
open fun detachAndScrapViewAt(index: Int, @NonNull recycler: @NonNull RecyclerView.Recycler)
Detach a child view and add it to a Recycler's scrap heap.
Link copied to clipboard
open fun detachView(@NonNull child: @NonNull View)
Temporarily detach a child view.
Link copied to clipboard
open fun detachViewAt(index: Int)
Temporarily detach a child view.
Link copied to clipboard
open fun endAnimation(view: View)
Ends all animations on the view created by the ItemAnimator.
Link copied to clipboard
@Nullable
open fun findContainingItemView(@NonNull view: @NonNull View): @Nullable View
Traverses the ancestors of the given view and returns the item view that contains it and also a direct child of the LayoutManager.
Link copied to clipboard
@Nullable
open fun findViewByPosition(position: Int): @Nullable View
Finds the view which represents the given adapter position.
Link copied to clipboard
Create a default LayoutParams object for a child of the RecyclerView.
Link copied to clipboard
Create a LayoutParams object suitable for this LayoutManager, copying relevant values from the supplied LayoutParams object if possible.
Create a LayoutParams object suitable for this LayoutManager from an inflated layout resource.
Link copied to clipboard
open fun getBaseline(): Int
Returns offset of the RecyclerView's text baseline from the its top boundary.
Link copied to clipboard
open fun getBottomDecorationHeight(@NonNull child: @NonNull View): Int
Returns the total height of item decorations applied to child's bottom.
Link copied to clipboard
@Nullable
open fun getChildAt(index: Int): @Nullable View
Return the child view at the given index
Link copied to clipboard
open fun getChildCount(): Int
Return the current number of child views attached to the parent RecyclerView.
Link copied to clipboard
open fun getChildMeasureSpec(parentSize: Int, padding: Int, childDimension: Int, canScroll: Boolean): Int
open fun getChildMeasureSpec(parentSize: Int, parentMode: Int, padding: Int, childDimension: Int, canScroll: Boolean): Int
Calculate a MeasureSpec value for measuring a child view in one dimension.
Link copied to clipboard
Check if the RecyclerView is configured to clip child views to its padding.
Link copied to clipboard
open fun getColumnCountForAccessibility(@NonNull recycler: @NonNull RecyclerView.Recycler, @NonNull state: @NonNull RecyclerView.State): Int
Returns the number of columns for accessibility.
Link copied to clipboard
open fun getDecoratedBottom(@NonNull child: @NonNull View): Int
Returns the bottom edge of the given child view within its parent, offset by any applied ItemDecorations.
Link copied to clipboard
open fun getDecoratedBoundsWithMargins(@NonNull view: @NonNull View, @NonNull outBounds: @NonNull Rect)
Returns the bounds of the view including its decoration and margins.
Link copied to clipboard
open fun getDecoratedLeft(@NonNull child: @NonNull View): Int
Returns the left edge of the given child view within its parent, offset by any applied ItemDecorations.
Link copied to clipboard
open fun getDecoratedMeasuredHeight(@NonNull child: @NonNull View): Int
Returns the measured height of the given child, plus the additional size of any insets applied by ItemDecorations.
Link copied to clipboard
open fun getDecoratedMeasuredWidth(@NonNull child: @NonNull View): Int
Returns the measured width of the given child, plus the additional size of any insets applied by ItemDecorations.
Link copied to clipboard
open fun getDecoratedRight(@NonNull child: @NonNull View): Int
Returns the right edge of the given child view within its parent, offset by any applied ItemDecorations.
Link copied to clipboard
open fun getDecoratedTop(@NonNull child: @NonNull View): Int
Returns the top edge of the given child view within its parent, offset by any applied ItemDecorations.
Link copied to clipboard
@Nullable
open fun getFocusedChild(): @Nullable View
Returns the item View which has or contains focus.
Link copied to clipboard
open fun getHeight(): Int
Returns the height that is currently relevant to the LayoutManager.
Link copied to clipboard
open fun getHeightMode(): Int
Return the height measurement spec mode that is currently relevant to the LayoutManager.
Link copied to clipboard
open fun getItemCount(): Int
Returns the number of items in the adapter bound to the parent RecyclerView.
Link copied to clipboard
open fun getItemViewType(@NonNull view: @NonNull View): Int
Returns the View type defined by the adapter.
Link copied to clipboard
Returns the resolved layout direction for this RecyclerView.
Link copied to clipboard
open fun getLeftDecorationWidth(@NonNull child: @NonNull View): Int
Returns the total width of item decorations applied to child's left.
Link copied to clipboard
open fun getMinimumHeight(): Int
Link copied to clipboard
open fun getMinimumWidth(): Int
Link copied to clipboard
open fun getPaddingBottom(): Int
Return the bottom padding of the parent RecyclerView
Link copied to clipboard
open fun getPaddingEnd(): Int
Return the end padding of the parent RecyclerView
Link copied to clipboard
open fun getPaddingLeft(): Int
Return the left padding of the parent RecyclerView
Link copied to clipboard
open fun getPaddingRight(): Int
Return the right padding of the parent RecyclerView
Link copied to clipboard
open fun getPaddingStart(): Int
Return the start padding of the parent RecyclerView
Link copied to clipboard
open fun getPaddingTop(): Int
Return the top padding of the parent RecyclerView
Link copied to clipboard
open fun getPosition(@NonNull view: @NonNull View): Int
Returns the adapter position of the item represented by the given View.
Link copied to clipboard
open fun getProperties(@NonNull context: @NonNull Context, @Nullable attrs: @Nullable AttributeSet, defStyleAttr: Int, defStyleRes: Int): RecyclerView.LayoutManager.Properties
Parse the xml attributes to get the most common properties used by layout managers.
Link copied to clipboard
open fun getRightDecorationWidth(@NonNull child: @NonNull View): Int
Returns the total width of item decorations applied to child's right.
Link copied to clipboard
open fun getRowCountForAccessibility(@NonNull recycler: @NonNull RecyclerView.Recycler, @NonNull state: @NonNull RecyclerView.State): Int
Returns the number of rows for accessibility.
Link copied to clipboard
open fun getSelectionModeForAccessibility(@NonNull recycler: @NonNull RecyclerView.Recycler, @NonNull state: @NonNull RecyclerView.State): Int
Returns the selection mode for accessibility.
Link copied to clipboard
open fun getTopDecorationHeight(@NonNull child: @NonNull View): Int
Returns the total height of item decorations applied to child's top.
Link copied to clipboard
open fun getTransformedBoundingBox(@NonNull child: @NonNull View, includeDecorInsets: Boolean, @NonNull out: @NonNull Rect)
Calculates the bounding box of the View while taking into account its matrix changes (translation, scale etc) with respect to the RecyclerView.
Link copied to clipboard
open fun getWidth(): Int
Returns the width that is currently relevant to the LayoutManager.
Link copied to clipboard
open fun getWidthMode(): Int
Return the width measurement spec mode that is currently relevant to the LayoutManager.
Link copied to clipboard
open fun hasFocus(): Boolean
Returns true if the RecyclerView this LayoutManager is bound to has or contains focus.
Link copied to clipboard
open fun ignoreView(@NonNull view: @NonNull View)
Flags a view so that it will not be scrapped or recycled.
Link copied to clipboard
Returns whether LayoutManager is currently attached to a RecyclerView which is attached to a window.
Link copied to clipboard
Returns whether the measuring pass of layout should use the AutoMeasure mechanism of RecyclerView or if it should be done by the LayoutManager's implementation of onMeasure.
Link copied to clipboard
open fun isFocused(): Boolean
Returns true if the RecyclerView this LayoutManager is bound to has focus.
Link copied to clipboard
Sets whether the LayoutManager should be queried for views outside of its viewport while the UI thread is idle between frames.
Link copied to clipboard
open fun isLayoutHierarchical(@NonNull recycler: @NonNull RecyclerView.Recycler, @NonNull state: @NonNull RecyclerView.State): Boolean
Returns whether layout is hierarchical or not to be used for accessibility.
Link copied to clipboard
Query if the layout is in reverse order.
Link copied to clipboard
In addition to the View Framework's measurement cache, RecyclerView uses its own additional measurement cache for its children to avoid re-measuring them when not necessary.
Link copied to clipboard
Link copied to clipboard
open fun isViewPartiallyVisible(@NonNull child: @NonNull View, completelyVisible: Boolean, acceptEndPointInclusion: Boolean): Boolean
Returns whether the given child view is partially or fully visible within the padded bounded area of RecyclerView, depending on the input parameters.
Link copied to clipboard
open fun layoutDecorated(@NonNull child: @NonNull View, left: Int, top: Int, right: Int, bottom: Int)
Lay out the given child view within the RecyclerView using coordinates that include any current ItemDecorations.
Link copied to clipboard
open fun layoutDecoratedWithMargins(@NonNull child: @NonNull View, left: Int, top: Int, right: Int, bottom: Int)
Lay out the given child view within the RecyclerView using coordinates that include any current ItemDecorations and margins.
Link copied to clipboard
open fun measureChild(@NonNull child: @NonNull View, widthUsed: Int, heightUsed: Int)
Measure a child view using standard measurement policy, taking the padding of the parent RecyclerView and any added item decorations into account.
Link copied to clipboard
open fun measureChildWithMargins(@NonNull child: @NonNull View, widthUsed: Int, heightUsed: Int)
Measure a child view using standard measurement policy, taking the padding of the parent RecyclerView, any added item decorations and the child margins into account.
Link copied to clipboard
open fun moveView(fromIndex: Int, toIndex: Int)
Moves a View from one position to another.
Link copied to clipboard
Offset all child views attached to the parent RecyclerView by dx pixels along the horizontal axis.
Link copied to clipboard
open fun offsetChildrenVertical(@Px dy: Int)
Offset all child views attached to the parent RecyclerView by dy pixels along the vertical axis.
Link copied to clipboard
open fun onAdapterChanged(@Nullable oldAdapter: @Nullable RecyclerView.Adapter, @Nullable newAdapter: @Nullable RecyclerView.Adapter<out RecyclerView.ViewHolder>)
Called if the RecyclerView this LayoutManager is bound to has a different adapter set via setAdapter or swapAdapter.
Link copied to clipboard
open fun onAddFocusables(@NonNull recyclerView: @NonNull RecyclerView, @NonNull views: @NonNull ArrayList<View>, direction: Int, focusableMode: Int): Boolean
Called to populate focusable views within the RecyclerView.
Link copied to clipboard
Called when this LayoutManager is both attached to a RecyclerView and that RecyclerView is attached to a window.
Link copied to clipboard

Called when this LayoutManager is detached from its parent RecyclerView or when its parent RecyclerView is detached from its window.
Link copied to clipboard
@Nullable
open fun onFocusSearchFailed(@NonNull focused: @NonNull View, direction: Int, @NonNull recycler: @NonNull RecyclerView.Recycler, @NonNull state: @NonNull RecyclerView.State): @Nullable View
Called when searching for a focusable view in the given direction has failed for the current content of the RecyclerView.
Link copied to clipboard
open fun onInitializeAccessibilityEvent(@NonNull event: @NonNull AccessibilityEvent)

open fun onInitializeAccessibilityEvent(@NonNull recycler: @NonNull RecyclerView.Recycler, @NonNull state: @NonNull RecyclerView.State, @NonNull event: @NonNull AccessibilityEvent)
Called by the accessibility delegate to initialize an accessibility event.
Link copied to clipboard
open fun onInitializeAccessibilityNodeInfo(@NonNull recycler: @NonNull RecyclerView.Recycler, @NonNull state: @NonNull RecyclerView.State, @NonNull info: @NonNull AccessibilityNodeInfoCompat)
Called by the AccessibilityDelegate when the information about the current layout should be populated.
Link copied to clipboard
open fun onInitializeAccessibilityNodeInfoForItem(@NonNull recycler: @NonNull RecyclerView.Recycler, @NonNull state: @NonNull RecyclerView.State, @NonNull host: @NonNull View, @NonNull info: @NonNull AccessibilityNodeInfoCompat)
Called by the AccessibilityDelegate when the accessibility information for a specific item should be populated.
Link copied to clipboard
@Nullable
open fun onInterceptFocusSearch(@NonNull focused: @NonNull View, direction: Int): @Nullable View
This method gives a LayoutManager an opportunity to intercept the initial focus search before the default behavior of FocusFinder is used.
Link copied to clipboard
open fun onItemsAdded(@NonNull recyclerView: @NonNull RecyclerView, positionStart: Int, itemCount: Int)
Called when items have been added to the adapter.
Link copied to clipboard
open fun onItemsChanged(@NonNull recyclerView: @NonNull RecyclerView)
Called in response to a call to notifyDataSetChanged or swapAdapter ()} and signals that the the entire data set has changed.
Link copied to clipboard
open fun onItemsMoved(@NonNull recyclerView: @NonNull RecyclerView, from: Int, to: Int, itemCount: Int)
Called when an item is moved withing the adapter.
Link copied to clipboard
open fun onItemsRemoved(@NonNull recyclerView: @NonNull RecyclerView, positionStart: Int, itemCount: Int)
Called when items have been removed from the adapter.
Link copied to clipboard
open fun onItemsUpdated(@NonNull recyclerView: @NonNull RecyclerView, positionStart: Int, itemCount: Int)
Called when items have been changed in the adapter.
open fun onItemsUpdated(@NonNull recyclerView: @NonNull RecyclerView, positionStart: Int, itemCount: Int, @Nullable payload: @Nullable Any)
Called when items have been changed in the adapter and with optional payload.
Link copied to clipboard
Lay out all relevant child views from the given adapter.
Link copied to clipboard
Called after a full layout calculation is finished.
Link copied to clipboard
open fun onMeasure(@NonNull recycler: @NonNull RecyclerView.Recycler, @NonNull state: @NonNull RecyclerView.State, widthSpec: Int, heightSpec: Int)
Measure the attached RecyclerView.
Link copied to clipboard
open fun onRequestChildFocus(@NonNull parent: @NonNull RecyclerView, @NonNull child: @NonNull View, @Nullable focused: @Nullable View): Boolean

open fun onRequestChildFocus(@NonNull parent: @NonNull RecyclerView, @NonNull state: @NonNull RecyclerView.State, @NonNull child: @NonNull View, @Nullable focused: @Nullable View): Boolean
Called when a descendant view of the RecyclerView requests focus.
Link copied to clipboard
Called when the RecyclerView is ready to restore the state based on a previous RecyclerView.
Link copied to clipboard
@Nullable
open fun onSaveInstanceState(): @Nullable Parcelable
Called when the LayoutManager should save its state.
Link copied to clipboard
open fun onScrollStateChanged(state: Int)
RecyclerView calls this method to notify LayoutManager that scroll state has changed.
Link copied to clipboard
open fun performAccessibilityAction(@NonNull recycler: @NonNull RecyclerView.Recycler, @NonNull state: @NonNull RecyclerView.State, action: Int, @Nullable args: @Nullable Bundle): Boolean
Called by AccessibilityDelegate when an action is requested from the RecyclerView.
Link copied to clipboard
open fun performAccessibilityActionForItem(@NonNull recycler: @NonNull RecyclerView.Recycler, @NonNull state: @NonNull RecyclerView.State, @NonNull view: @NonNull View, action: Int, @Nullable args: @Nullable Bundle): Boolean
Called by AccessibilityDelegate when an accessibility action is requested on one of the children of LayoutManager.
Link copied to clipboard
open fun postOnAnimation(action: Runnable)
Causes the Runnable to execute on the next animation time step.
Link copied to clipboard
open fun removeAllViews()
Remove all views from the currently attached RecyclerView.
Link copied to clipboard
open fun removeAndRecycleAllViews(@NonNull recycler: @NonNull RecyclerView.Recycler)
Removes all views and recycles them using the given recycler.
Link copied to clipboard
open fun removeAndRecycleView(@NonNull child: @NonNull View, @NonNull recycler: @NonNull RecyclerView.Recycler)
Remove a child view and recycle it using the given Recycler.
Link copied to clipboard
open fun removeAndRecycleViewAt(index: Int, @NonNull recycler: @NonNull RecyclerView.Recycler)
Remove a child view and recycle it using the given Recycler.
Link copied to clipboard
open fun removeCallbacks(action: Runnable): Boolean
Removes the specified Runnable from the message queue.
Link copied to clipboard
open fun removeDetachedView(@NonNull child: @NonNull View)
Finish removing a view that was previously temporarily detached.
Link copied to clipboard
open fun removeView(child: View)
Remove a view from the currently attached RecyclerView if needed.
Link copied to clipboard
open fun removeViewAt(index: Int)
Remove a view from the currently attached RecyclerView if needed.
Link copied to clipboard
open fun requestChildRectangleOnScreen(@NonNull parent: @NonNull RecyclerView, @NonNull child: @NonNull View, @NonNull rect: @NonNull Rect, immediate: Boolean): Boolean
Called when a child of the RecyclerView wants a particular rectangle to be positioned onto the screen.
open fun requestChildRectangleOnScreen(@NonNull parent: @NonNull RecyclerView, @NonNull child: @NonNull View, @NonNull rect: @NonNull Rect, immediate: Boolean, focusedChildVisible: Boolean): Boolean
Requests that the given child of the RecyclerView be positioned onto the screen.
Link copied to clipboard
open fun requestLayout()
Calls RecyclerView#requestLayout on the underlying RecyclerView
Link copied to clipboard
A LayoutManager can call this method to force RecyclerView to run simple animations in the next layout pass, even if there is not any trigger to do so.
Link copied to clipboard
Scroll horizontally by dx pixels in screen coordinates and return the distance traveled.
Link copied to clipboard
open fun scrollToPosition(position: Int)
Scroll to the specified adapter position.
Link copied to clipboard
Scroll vertically by dy pixels in screen coordinates and return the distance traveled.
Link copied to clipboard
open fun setAutoMeasureEnabled(enabled: Boolean)
Defines whether the measuring pass of layout should use the AutoMeasure mechanism of RecyclerView or if it should be done by the LayoutManager's implementation of onMeasure.
Link copied to clipboard
Sets whether the LayoutManager should be queried for views outside of its viewport while the UI thread is idle between frames.
Link copied to clipboard
open fun setMeasuredDimension(widthSize: Int, heightSize: Int)
Set the measured dimensions of the host RecyclerView.
open fun setMeasuredDimension(childrenBounds: Rect, wSpec: Int, hSpec: Int)
Sets the measured dimensions from the given bounding box of the children and the measurement specs that were passed into onMeasure.
Link copied to clipboard
open fun setMeasurementCacheEnabled(measurementCacheEnabled: Boolean)
Sets whether RecyclerView should use its own measurement cache for the children.
Link copied to clipboard
open fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State, position: Int)
Smooth scroll to the specified adapter position.
Link copied to clipboard
Starts a smooth scroll using the provided SmoothScroller.
Link copied to clipboard
open fun stopIgnoringView(@NonNull view: @NonNull View)
View can be scrapped and recycled again.
Link copied to clipboard
Returns whether this LayoutManager supports "predictive item animations".