RecyclerView
open class RecyclerView : ViewGroup, ScrollingView, NestedScrollingChild2, NestedScrollingChild3
kotlin.Any | |||
↳ | android.view.View | ||
↳ | android.view.ViewGroup | ||
↳ | androidx.recyclerview.widget.RecyclerView |
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
ViewGroup#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. ViewHolder#getLayoutPosition()
, findViewHolderForLayoutPosition(int)
). 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. ViewHolder#getAbsoluteAdapterPosition()
, ViewHolder#getBindingAdapterPosition()
, findViewHolderForAdapterPosition(int)
) 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 ViewHolder#getBindingAdapterPosition()
. Beware that these methods may not be able to calculate adapter positions if Adapter#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 callingadapter.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 useSortedList
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 SortedList#replaceAll(Object[])
, 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 theandroidx.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. androidx.recyclerview.R.attr#layoutManager
Summary
Nested classes | |
---|---|
abstract |
Base class for an Adapter |
abstract |
Observer base class for watching changes to an |
abstract |
A callback interface that can be used to alter the drawing order of RecyclerView children. |
open |
EdgeEffectFactory lets you customize the over-scroll edge effect for RecyclerViews. |
abstract |
This class defines the animations that take place on items as changes are made to the adapter. |
abstract |
An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set. |
abstract |
A |
open |
|
abstract |
A Listener interface that can be attached to a RecylcerView to get notified whenever a ViewHolder is attached to or detached from RecyclerView. |
abstract |
This class defines the behavior of fling if the developer wishes to handle it. |
abstract |
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. |
abstract |
An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event has occurred on that RecyclerView. |
open |
RecycledViewPool lets you share Views between multiple RecyclerViews. |
A Recycler is responsible for managing scrapped or detached item views for reuse. |
|
abstract |
A RecyclerListener can be set on a RecyclerView to receive messages whenever a view is recycled. |
open |
An implementation of |
abstract |
Base class for smooth scrolling. |
open |
Contains useful information about the current RecyclerView state like target scroll position or view focus. |
abstract |
ViewCacheExtension is a helper class to provide an additional layer of view caching that can be controlled by the developer. |
abstract |
A ViewHolder describes an item view and metadata about its place within the RecyclerView. |
Constants | |
---|---|
static Int | |
static Int | |
static Long | |
static Int | |
static Int |
The RecyclerView is currently being dragged by outside input such as user touch input. |
static Int |
The RecyclerView is not currently scrolling. |
static Int |
The RecyclerView is currently animating to a final position while not under outside control. |
static Int |
Constant for use with |
static Int |
Constant for use with |
static Int |
Constant that represents that a duration has not been defined. |
static Int |
Public constructors | |
---|---|
<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?) |
|
<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int) |
Public methods | |
---|---|
open Unit |
addFocusables(views: ArrayList<View!>!, direction: Int, focusableMode: Int) |
open Unit |
addItemDecoration(@NonNull decor: RecyclerView.ItemDecoration, index: Int) Add an |
open Unit |
addItemDecoration(@NonNull decor: RecyclerView.ItemDecoration) Add an |
open Unit |
addOnChildAttachStateChangeListener(@NonNull listener: RecyclerView.OnChildAttachStateChangeListener) Register a listener that will be notified whenever a child view is attached to or detached from RecyclerView. |
open Unit |
addOnItemTouchListener(@NonNull listener: RecyclerView.OnItemTouchListener) Add an |
open Unit |
addOnScrollListener(@NonNull listener: RecyclerView.OnScrollListener) Add a listener that will be notified of any changes in scroll state or position. |
open Unit |
addRecyclerListener(@NonNull listener: RecyclerView.RecyclerListener) Register a listener that will be notified whenever a child view is recycled. |
open Unit |
Removes all listeners that were added |