RecyclerView

(SESL variant) A flexible view for providing a limited window into a large data set.

Glossary of terms:

  • Adapter: A subclass of Adapter responsible for providing views that represent items in a data set.
  • Position: The position of a data item within an Adapter.
  • Index: The index of an attached child view as used in a call to getChildAt. Contrast with Position.
  • Binding: The process of preparing a child view to display data corresponding to a position within the adapter.
  • Recycle (view): A view previously used to display data for a specific adapter position may be placed in a cache for later reuse to display the same type of data again later. This can drastically improve performance by skipping initial layout inflation or construction.
  • Scrap (view): A child view that has entered into a temporarily detached state during layout. Scrap views may be reused without becoming fully detached from the parent RecyclerView, either unmodified if no rebinding is required or modified by the adapter if the view was considered dirty.
  • Dirty (view): A child view that must be rebound by the adapter before being displayed.

Positions in RecyclerView:

RecyclerView introduces an additional level of abstraction between the Adapter and LayoutManager to be able to detect data set changes in batches during a layout calculation. This saves LayoutManager from tracking adapter changes to calculate animations. It also helps with performance because all view bindings happen at the same time and unnecessary bindings are avoided.

For this reason, there are two types of position related methods in RecyclerView:

  • layout position: Position of an item in the latest layout calculation. This is the position from the LayoutManager's perspective.
  • adapter position: Position of an item in the adapter. This is the position from the Adapter's perspective.

These two positions are the same except the time between dispatching adapter.notify* events and calculating the updated layout.

Methods that return or receive *LayoutPosition* use position as of the latest layout calculation (e.g. getLayoutPosition, findViewHolderForLayoutPosition). These positions include all changes until the last layout calculation. You can rely on these positions to be consistent with what user is currently seeing on the screen. For example, if you have a list of items on the screen and user asks for the 5th element, you should use these methods as they'll match what user is seeing.

The other set of position related methods are in the form of *AdapterPosition*. (e.g. getAbsoluteAdapterPosition, getBindingAdapterPosition, findViewHolderForAdapterPosition) You should use these methods when you need to work with up-to-date adapter positions even if they may not have been reflected to layout yet. For example, if you want to access the item in the adapter on a ViewHolder click, you should use getBindingAdapterPosition. Beware that these methods may not be able to calculate adapter positions if notifyDataSetChanged has been called and new layout has not yet been calculated. For this reasons, you should carefully handle NO_POSITION or null results from these methods.

When writing a LayoutManager you almost always want to use layout positions whereas when writing an Adapter, you probably want to use adapter positions.

Presenting Dynamic Data

To display updatable data in a RecyclerView, your adapter needs to signal inserts, moves, and deletions to RecyclerView. You can build this yourself by manually calling adapter.notify* methods when content changes, or you can use one of the easier solutions RecyclerView provides: List diffing with DiffUtil If your RecyclerView is displaying a list that is re-fetched from scratch for each update (e.g. from the network, or from a database), DiffUtil can calculate the difference between versions of the list. DiffUtil takes both lists as input and computes the difference, which can be passed to RecyclerView to trigger minimal animations and updates to keep your UI performant, and animations meaningful. This approach requires that each list is represented in memory with immutable content, and relies on receiving updates as new instances of lists. This approach is also ideal if your UI layer doesn't implement sorting, it just presents the data in the order it's given.

The best part of this approach is that it extends to any arbitrary changes - item updates, moves, addition and removal can all be computed and handled the same way. Though you do have to keep two copies of the list in memory while diffing, and must avoid mutating them, it's possible to share unmodified elements between list versions.

There are three primary ways to do this for RecyclerView. We recommend you start with ListAdapter, the higher-level API that builds in List diffing on a background thread, with minimal code. AsyncListDiffer also provides this behavior, but without defining an Adapter to subclass. If you want more control, DiffUtil is the lower-level API you can use to compute the diffs yourself. Each approach allows you to specify how diffs should be computed based on item data.

List mutation with SortedList If your RecyclerView receives updates incrementally, e.g. item X is inserted, or item Y is removed, you can use SortedList to manage your list. You define how to order items, and it will automatically trigger update signals that RecyclerView can use. SortedList works if you only need to handle insert and remove events, and has the benefit that you only ever need to have a single copy of the list in memory. It can also compute differences with replaceAll, but this method is more limited than the list diffing behavior above. Paging Library The Paging library extends the diff-based approach to additionally support paged loading. It provides the androidx.paging.PagedList class that operates as a self-loading list, provided a source of data like a database, or paginated network API. It provides convenient list diffing support out of the box, similar to ListAdapter and AsyncListDiffer. For more information about the Paging library, see the library documentation. layoutManager

Constructors

Link copied to clipboard
constructor(@NonNull context: Context)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet, defStyleAttr: Int)

Types

Link copied to clipboard
Base class for an Adapter Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView.
Link copied to clipboard
abstract class AdapterDataObserver
Observer base class for watching changes to an Adapter.
Link copied to clipboard
A callback interface that can be used to alter the drawing order of RecyclerView children.
Link copied to clipboard
EdgeEffectFactory lets you customize the over-scroll edge effect for RecyclerViews.
Link copied to clipboard
abstract class ItemAnimator
This class defines the animations that take place on items as changes are made to the adapter.
Link copied to clipboard
abstract class ItemDecoration
An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set.
Link copied to clipboard
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.
Link copied to clipboard
Link copied to clipboard
A Listener interface that can be attached to a RecylcerView to get notified whenever a ViewHolder is attached to or detached from RecyclerView.
Link copied to clipboard
abstract class OnFlingListener
This class defines the behavior of fling if the developer wishes to handle it.
Link copied to clipboard
An OnItemTouchListener allows the application to intercept touch events in progress at the view hierarchy level of the RecyclerView before those touch events are considered for RecyclerView's own scrolling behavior.
Link copied to clipboard
abstract class OnScrollListener
An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event has occurred on that RecyclerView.
Link copied to clipboard
Link copied to clipboard
open class RecycledViewPool
RecycledViewPool lets you share Views between multiple RecyclerViews.
Link copied to clipboard
inner class Recycler
A Recycler is responsible for managing scrapped or detached item views for reuse.
Link copied to clipboard
A RecyclerListener can be set on a RecyclerView to receive messages whenever a view is recycled.
Link copied to clipboard
This is public so that the CREATOR can be accessed on cold launch.
Link copied to clipboard
Interface to receive notifications of the fast scroller events.
Link copied to clipboard
Interface definition for a callback to be invoked when a long-press multi selection events.
Link copied to clipboard
Interface definition for a callback to be invoked when the "Go to top" button is clicked.
Link copied to clipboard
Interface definition for a callback to be invoked when a multi-selection event occurs using either a stylus or mouse input.
Link copied to clipboard
An implementation of RecyclerView.OnItemTouchListener that has empty method bodies and default return values.
Link copied to clipboard
abstract class SmoothScroller
Base class for smooth scrolling.
Link copied to clipboard
open class State
Contains useful information about the current RecyclerView state like target scroll position or view focus.
Link copied to clipboard
abstract class ViewCacheExtension
ViewCacheExtension is a helper class to provide an additional layer of view caching that can be controlled by the developer.
Link copied to clipboard
abstract class ViewHolder
A ViewHolder describes an item view and metadata about its place within the RecyclerView.

Properties

Link copied to clipboard
val HORIZONTAL: Int = 0
Link copied to clipboard
val INVALID_TYPE: Int = -1
Link copied to clipboard
val NO_ID: Long = -1
Link copied to clipboard
val NO_POSITION: Int = -1
Link copied to clipboard
The RecyclerView is currently being dragged by outside input such as user touch input.
Link copied to clipboard
The RecyclerView is not currently scrolling.
Link copied to clipboard
The RecyclerView is currently animating to a final position while not under outside control.
Link copied to clipboard
Constant for use with setScrollingTouchSlop.
Link copied to clipboard
Constant for use with setScrollingTouchSlop.
Link copied to clipboard
val UNDEFINED_DURATION: Int = -2147483648
Constant that represents that a duration has not been defined.
Link copied to clipboard
val VERTICAL: Int = 1

Functions

Link copied to clipboard
open fun addFocusables(views: ArrayList<View>, direction: Int, focusableMode: Int)
Link copied to clipboard
Add an ItemDecoration to this RecyclerView.
Link copied to clipboard
Register a listener that will be notified whenever a child view is attached to or detached from RecyclerView.
Link copied to clipboard
Add an OnItemTouchListener to intercept touch events before they are dispatched to child views or this view's standard scrolling behavior.
Link copied to clipboard
Add a listener that will be notified of any changes in scroll state or position.
Link copied to clipboard
Register a listener that will be notified whenever a child view is recycled.
Link copied to clipboard
Removes all listeners that were added via addOnChildAttachStateChangeListener.
Link copied to clipboard
Remove all secondary listener that were notified of any changes in scroll state or position.
Link copied to clipboard
Compute the horizontal extent of the horizontal scrollbar's thumb within the horizontal range.
Link copied to clipboard
Compute the horizontal offset of the horizontal scrollbar's thumb within the horizontal range.
Link copied to clipboard
Compute the horizontal range that the horizontal scrollbar represents.
Link copied to clipboard
Compute the vertical extent of the vertical scrollbar's thumb within the vertical range.
Link copied to clipboard
Compute the vertical offset of the vertical scrollbar's thumb within the vertical range.
Link copied to clipboard
Compute the vertical range that the vertical scrollbar represents.
Link copied to clipboard
Link copied to clipboard
open fun dispatchNestedFling(velocityX: Float, velocityY: Float, consumed: Boolean): Boolean
Link copied to clipboard
open fun dispatchNestedPreFling(velocityX: Float, velocityY: Float): Boolean
Link copied to clipboard
open fun dispatchNestedPreScroll(dx: Int, dy: Int, consumed: Array<Int>, offsetInWindow: Array<Int>): Boolean
open fun dispatchNestedPreScroll(dx: Int, dy: Int, consumed: Array<Int>, offsetInWindow: Array<Int>, type: Int): Boolean
Link copied to clipboard
open fun dispatchNestedScroll(dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, offsetInWindow: Array<Int>): Boolean
open fun dispatchNestedScroll(dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, offsetInWindow: Array<Int>, type: Int): Boolean
fun dispatchNestedScroll(dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, offsetInWindow: Array<Int>, type: Int, @NonNull consumed: Array<Int>)
Link copied to clipboard
Link copied to clipboard
open fun draw(@NonNull c: Canvas)
Link copied to clipboard
open fun drawChild(@NonNull canvas: Canvas, child: View, drawingTime: Long): Boolean
Link copied to clipboard
Find the topmost view under the given point.
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 RecyclerView.
Link copied to clipboard
Returns the ViewHolder that contains the given view.
Link copied to clipboard
Return the ViewHolder for the item in the given position of the data set.
Link copied to clipboard
Return the ViewHolder for the item with the given id.
Link copied to clipboard
Return the ViewHolder for the item in the given position of the data set as of the latest layout pass.
Link copied to clipboard
open fun fling(velocityX: Int, velocityY: Int): Boolean
Begin a standard fling with an initial velocity along each axis in pixels per second.
Link copied to clipboard
open fun focusSearch(focused: View, direction: Int): View
Since RecyclerView is a collection ViewGroup that includes virtual children (items that are in the Adapter but not visible in the UI), it employs a more involved focus search strategy that differs from other ViewGroups.
Link copied to clipboard
Retrieves the previously set adapter or null if no adapter is set.
Link copied to clipboard
open fun getBaseline(): Int
Return the offset of the RecyclerView's text baseline from the its top boundary.
Link copied to clipboard
Return the adapter position that the given child view corresponds to.
Link copied to clipboard
open fun getChildItemId(@NonNull child: View): Long
Return the stable item id that the given child view corresponds to.
Link copied to clipboard
Return the adapter position of the given child view as of the latest completed layout pass.
Link copied to clipboard
open fun getChildPosition(@NonNull child: View): Int
Link copied to clipboard
Retrieve the ViewHolder for the given child view.
Link copied to clipboard
Returns whether this RecyclerView will clip its children to its padding, and resize (but not clip) any EdgeEffect to the padded region, if padding is present.
Link copied to clipboard
Returns the accessibility delegate compatibility implementation used by the RecyclerView.
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
Retrieves the previously set EdgeEffectFactory or the default factory if nothing was set.
Link copied to clipboard
Gets the current ItemAnimator for this RecyclerView.
Link copied to clipboard
Returns an ItemDecoration previously added to this RecyclerView.
Link copied to clipboard
Returns the number of ItemDecoration currently added to this RecyclerView.
Link copied to clipboard
Return the LayoutManager currently responsible for layout policy for this RecyclerView.
Link copied to clipboard
Returns the long press multi selection listener for the RecyclerView.
Link copied to clipboard
Returns the maximum fling velocity used by this RecyclerView.
Link copied to clipboard
Returns the minimum velocity to start a fling.
Link copied to clipboard
Link copied to clipboard
Returns true if the RecyclerView should attempt to preserve currently focused Adapter Item's focus even if the View representing the Item is replaced during a layout calculation.
Link copied to clipboard
Link copied to clipboard
open fun getScrollState(): Int
Return the current scrolling state of the RecyclerView.
Link copied to clipboard
open fun hasFixedSize(): Boolean
Link copied to clipboard
Link copied to clipboard
Returns whether there are pending adapter updates which are not yet applied to the layout.
Link copied to clipboard
Invalidates all ItemDecorations.
Link copied to clipboard
open fun isAnimating(): Boolean
Returns true if RecyclerView is currently running some animations.
Link copied to clipboard
Returns true if RecyclerView is attached to window.
Link copied to clipboard
Returns whether RecyclerView is currently computing a layout.
Link copied to clipboard
Link copied to clipboard
Returns whether layout and scroll calls on this container are currently being suppressed, due to an earlier call to suppressLayout.
Link copied to clipboard
Link copied to clipboard
Indicates whether the vertical scrollbar is enabled.
Link copied to clipboard
open fun nestedScrollBy(x: Int, y: Int)
Same as scrollBy, but also participates in nested scrolling.
Link copied to clipboard
Offset the bounds of all child views by dx pixels.
Link copied to clipboard
open fun offsetChildrenVertical(@Px dy: Int)
Offset the bounds of all child views by dy pixels.
Link copied to clipboard
Called when an item view is attached to this RecyclerView.
Link copied to clipboard
Called when an item view is detached from this RecyclerView.
Link copied to clipboard
open fun onDraw(@NonNull c: Canvas)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean
Link copied to clipboard
open fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean
Link copied to clipboard
open fun onRtlPropertiesChanged(layoutDirection: Int)
Link copied to clipboard
open fun onScrolled(@Px dx: Int, @Px dy: Int)
Called when the scroll position of this RecyclerView changes.
Link copied to clipboard
open fun onScrollStateChanged(state: Int)
Called when the scroll state of this RecyclerView changes.
Link copied to clipboard
Link copied to clipboard
Remove an ItemDecoration from this RecyclerView.
Link copied to clipboard
open fun removeItemDecorationAt(index: Int)
Removes the ItemDecoration associated with the supplied index position.
Link copied to clipboard
Removes the provided listener from child attached state listeners list.
Link copied to clipboard
Remove a listener that was notified of any changes in scroll state or position.
Link copied to clipboard
Removes the provided listener from RecyclerListener list.
Link copied to clipboard
open fun requestChildFocus(child: View, focused: View)
Link copied to clipboard
open fun requestChildRectangleOnScreen(child: View, rect: Rect, immediate: Boolean): Boolean
Link copied to clipboard
open fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean)
Link copied to clipboard
open fun requestLayout()
Link copied to clipboard
open fun scrollBy(x: Int, y: Int)
Link copied to clipboard
open fun scrollTo(x: Int, y: Int)
Link copied to clipboard
open fun scrollToPosition(position: Int)
Convenience method to scroll to a certain position.
Link copied to clipboard
Finds the child view that is closest to the given coordinates.
Link copied to clipboard
Retrieves the bottom padding of the "Go To Top" button.
Link copied to clipboard
Retrieves the bottom area for hover input.
Link copied to clipboard
Retrieves the top area for hover input.
Link copied to clipboard
Returns true if fast scrolling is enabled, false otherwise.
Link copied to clipboard
Retrieves whether the index tip is enabled.
Link copied to clipboard
Checks whether the stylus input uses the page scrolling touch slop threshold when deciding if a gesture is a drag event.
Link copied to clipboard
open fun seslSetCtrlkeyPressed(pressed: Boolean)
Simulates a Ctrl key press.
Link copied to clipboard
Sets additional padding for the fast scroller.
Link copied to clipboard
Enables or disables the fast scroller.
Link copied to clipboard
Sets a listener to receive fast scroll events.
Link copied to clipboard
open fun seslSetFastScrollerThreshold(threshold: Float)
Sets the threshold for the fast scroller to start reacting on the touch motion event it receives.
Link copied to clipboard
Sets the custom color used to fill the bottom area and the bottom rounded corners when seslSetFillBottomEnabled is set to true.
Link copied to clipboard
Enables or disables filling the bottom area of the RecyclerView with a solid color when the content height is less than the viewport height.
Link copied to clipboard
Enables or disables horizontal padding fill and adjusts vertical scrollbar padding for the RecyclerView.
Link copied to clipboard
open fun seslSetGoToTopBottomPadding(padding: Int)
Sets a custom bottom padding of the "Go To Top" button.
Link copied to clipboard
open fun seslSetGoToTopEnabled(enable: Boolean)
Sets whether the "Go to top" button is enabled.
Link copied to clipboard
open fun seslSetHoverBottomPadding(padding: Int)
Sets the bottom area for hover input.
Link copied to clipboard
Enable or disable the hover scroll.
Link copied to clipboard
open fun seslSetHoverTopPadding(padding: Int)
Sets the top area for hover input.
Link copied to clipboard
Sets the bottom padding for immersive scroll.
Link copied to clipboard
open fun seslSetIndexTipEnabled(enabled: Boolean)
open fun seslSetIndexTipEnabled(enabled: Boolean, topMargin: Int)
Enables or disables the index tip.
Link copied to clipboard
Sets whether bottom rounded corner should be drawn on the last item.
Link copied to clipboard
Sets the listener that will be called when the user starts a multi-selection by long pressing on an item.
Link copied to clipboard
Register a callback to be invoked when the "Go To Top" button is clicked.
Link copied to clipboard
Sets whether to use the higher touch slop distance threshold for stylus events, which corresponds to page scrolling, before its motion event is considered to be dragging.
Link copied to clipboard
Sets whether pen selection is enabled.
Link copied to clipboard
open fun seslSetPointerIconRotation(rotation: Float)
Sets the rotation of the pointer icon.
Link copied to clipboard
open fun seslSetRecoilEnabled(enabled: Boolean)
Sets whether the recoil effect on items is enabled.
Link copied to clipboard
open fun seslSetScrollbarVerticalPadding(top: Int, bottom: Int)
Sets the vertical padding for the scrollbar.
Link copied to clipboard
Enables or disables smooth scrolling.
Link copied to clipboard
open fun seslShowGoToTopEdge(deltaDistance: Float, displacement: Float, delayTime: Int)
Link copied to clipboard
open fun seslSnapScrollToPosition(position: Int)
Smoothly scrolls to the specified adapter position.
Link copied to clipboard
Call to start a long-press multi-selection session.
Link copied to clipboard
Updates the position of the index tip.
Link copied to clipboard
Sets the accessibility delegate compatibility implementation used by RecyclerView.
Link copied to clipboard
Set a new adapter to provide child views on demand.
Link copied to clipboard
Sets the ChildDrawingOrderCallback to be used for drawing children.
Link copied to clipboard
open fun setClipToPadding(clipToPadding: Boolean)
Link copied to clipboard
open fun setDebugAssertionsEnabled(debugAssertionsEnabled: Boolean)
Enable internal assertions about RecyclerView's state and throw exceptions if the assertions are violated.
Link copied to clipboard
Link copied to clipboard
open fun setHasFixedSize(hasFixedSize: Boolean)
RecyclerView can perform several optimizations if it can know in advance that RecyclerView's size is not affected by the adapter contents.
Link copied to clipboard
Sets the ItemAnimator that will handle animations involving changes to the items in this RecyclerView.
Link copied to clipboard
open fun setItemViewCacheSize(size: Int)
Set the number of offscreen views to retain before adding them to the potentially shared recycled view pool.
Link copied to clipboard
open fun setLayoutFrozen(frozen: Boolean)
Enable or disable layout and scroll.
Link copied to clipboard
Set the LayoutManager that this RecyclerView will use.
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Set a listener that will be notified of any changes in scroll state or position.
Link copied to clipboard
open fun setPreserveFocusAfterLayout(preserveFocusAfterLayout: Boolean)
Set whether the RecyclerView should try to keep the same Item focused after a layout calculation or not.
Link copied to clipboard
Recycled view pools allow multiple RecyclerViews to share a common pool of scrap views.
Link copied to clipboard
Register a listener that will be notified whenever a child view is recycled.
Link copied to clipboard
open fun setScrollBarStyle(style: Int)
Link copied to clipboard
open fun setScrollingTouchSlop(slopConstant: Int)
Configure the scrolling touch slop for a specific use case.
Link copied to clipboard
open fun setVerboseLoggingEnabled(verboseLoggingEnabled: Boolean)
Enable verbose logging within RecyclerView itself.
Link copied to clipboard
Sets a new ViewCacheExtension to be used by the Recycler.
Link copied to clipboard
open fun smoothScrollBy(@Px dx: Int, @Px dy: Int)
open fun smoothScrollBy(@Px dx: Int, @Px dy: Int, @Nullable interpolator: Interpolator)
Animate a scroll by the given amount of pixels along either axis.
open fun smoothScrollBy(@Px dx: Int, @Px dy: Int, @Nullable interpolator: Interpolator, duration: Int)
Smooth scrolls the RecyclerView by a given distance.
Link copied to clipboard
open fun smoothScrollToPosition(position: Int)
Starts a smooth scroll to an adapter position.
Link copied to clipboard
open fun startNestedScroll(axes: Int): Boolean
open fun startNestedScroll(axes: Int, type: Int): Boolean
Link copied to clipboard
open fun stopNestedScroll()
open fun stopNestedScroll(type: Int)
Link copied to clipboard
open fun stopScroll()
Stop any current scroll in progress, such as one started by smoothScrollBy, fling or a touch-initiated fling.
Link copied to clipboard
fun suppressLayout(suppress: Boolean)
Tells this RecyclerView to suppress all layout and scroll calls until layout suppression is disabled with a later call to suppressLayout(false).
Link copied to clipboard
open fun swapAdapter(@Nullable adapter: RecyclerView.Adapter, removeAndRecycleExistingViews: Boolean)
Swaps the current adapter with the provided one.
Link copied to clipboard
open fun verifyDrawable(@NonNull drawable: Drawable): Boolean