LayoutManager

abstract class LayoutManager(source)

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: View)
open fun attachView(@NonNull child: View, index: Int)
open fun attachView(@NonNull child: View, index: Int, lp: RecyclerView.LayoutParams)
Reattach a previously detached view.
Link copied to clipboard
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
Override this method if you want to support scroll bars.
Link copied to clipboard
Override this method if you want to support scroll bars.
Link copied to clipboard
Override this method if you want to support scroll bars.
Link copied to clipboard
Override this method if you want to support scroll bars.
Link copied to clipboard
Override this method if you want to support scroll bars.
Link copied to clipboard
Override this method if you want to support scroll bars.
Link copied to clipboard
Temporarily detach and scrap all currently attached child views.
Link copied to clipboard
Detach a child view and add it to a Recycler's scrap heap.
Link copied to clipboard
Detach a child view and add it to a Recycler's scrap heap.
Link copied to clipboard
open fun detachView(@NonNull child: 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
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
open fun findViewByPosition(position: Int): 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
Returns the total height of item decorations applied to child's bottom.
Link copied to clipboard
open fun getChildAt(index: Int): 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
Returns the number of columns for accessibility.
Link copied to clipboard
open fun getDecoratedBottom(@NonNull child: 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: View, @NonNull outBounds: Rect)
Returns the bounds of the view including its decoration and margins.
Link copied to clipboard
open fun getDecoratedLeft(@NonNull child: View): Int
Returns the left edge of the given child view within its parent, offset by any applied ItemDecorations.
Link copied to clipboard
Returns the measured height of the given child, plus the additional size of any insets applied by ItemDecorations.
Link copied to clipboard
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: 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: View): Int
Returns the top edge of the given child view within its parent, offset by any applied ItemDecorations.
Link copied to clipboard
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: 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
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: View): Int
Returns the adapter position of the item represented by the given View.
Link copied to clipboard
open fun getProperties(@NonNull context: Context, @Nullable attrs: 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
Returns the total width of item decorations applied to child's right.
Link copied to clipboard
Returns the number of rows for accessibility.
Link copied to clipboard
Returns the selection mode for accessibility.
Link copied to clipboard
Returns the total height of item decorations applied to child's top.
Link copied to clipboard
open fun getTransformedBoundingBox(@NonNull child: View, includeDecorInsets: Boolean, @NonNull out: 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: 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
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: 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: 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: 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: 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: 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
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: RecyclerView, @NonNull views: 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
open fun onFocusSearchFailed(@NonNull focused: View, direction: Int, @NonNull recycler: RecyclerView.Recycler, @NonNull state: RecyclerView.State): 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

Called by the accessibility delegate to initialize an accessibility event.
Link copied to clipboard
Called by the AccessibilityDelegate when the information about the current layout should be populated.
Link copied to clipboard
Called by the AccessibilityDelegate when the accessibility information for a specific item should be populated.
Link copied to clipboard
open fun onInterceptFocusSearch(@NonNull focused: View, direction: Int): 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: RecyclerView, positionStart: Int, itemCount: Int)
Called when items have been added to the adapter.
Link copied to clipboard
open fun onItemsChanged(@NonNull recyclerView: 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: 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: RecyclerView, positionStart: Int, itemCount: Int)
Called when items have been removed from the adapter.
Link copied to clipboard
open fun onItemsUpdated(@NonNull recyclerView: RecyclerView, positionStart: Int, itemCount: Int)
Called when items have been changed in the adapter.
open fun onItemsUpdated(@NonNull recyclerView: RecyclerView, positionStart: Int, itemCount: Int, @Nullable payload: 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: RecyclerView.Recycler, @NonNull state: RecyclerView.State, widthSpec: Int, heightSpec: Int)
Measure the attached RecyclerView.
Link copied to clipboard
open fun onRequestChildFocus(@NonNull parent: RecyclerView, @NonNull child: View, @Nullable focused: View): Boolean

open fun onRequestChildFocus(@NonNull parent: RecyclerView, @NonNull state: RecyclerView.State, @NonNull child: View, @Nullable focused: 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
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
Called by AccessibilityDelegate when an action is requested from the RecyclerView.
Link copied to clipboard
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
Removes all views and recycles them using the given recycler.
Link copied to clipboard
Remove a child view and recycle it using the given Recycler.
Link copied to clipboard
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: 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: RecyclerView, @NonNull child: View, @NonNull rect: 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: RecyclerView, @NonNull child: View, @NonNull rect: 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: View)
View can be scrapped and recycled again.
Link copied to clipboard
Returns whether this LayoutManager supports "predictive item animations".