open class RecyclerView : ViewGroup, ScrollingView, NestedScrollingChild2, NestedScrollingChild3
A flexible view for providing a limited window into a large data set.
Glossary of terms:
- Adapter: A subclass of
Adapterresponsible 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
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.
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
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
null results from these methods.
Presenting Dynamic DataTo 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 DiffUtilIf your RecyclerView is displaying a list that is re-fetched from scratch for each update (e.g. from the network, or from a database),
DiffUtilcan calculate the difference between versions of the list.
DiffUtiltakes 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 SortedListIf your RecyclerView receives updates incrementally, e.g. item X is inserted, or item Y is removed, you can use
SortedListto 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 LibraryThe Paging library extends the diff-based approach to additionally support paged loading. It provides the
androidx.paging.PagedListclass 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
AsyncListDiffer. For more information about the Paging library, see the library documentation.
Base class for an Adapter
Observer base class for watching changes to an
A callback interface that can be used to alter the drawing order of RecyclerView children.
EdgeEffectFactory lets you customize the over-scroll edge effect for RecyclerViews.
This class defines the animations that take place on items as changes are made to the adapter.
An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set.
A Listener interface that can be attached to a RecylcerView to get notified whenever a ViewHolder is attached to or detached from RecyclerView.