RecyclerView is a view that allows you to view a portion of a large set of data.
It refers to several classes , namely RecyclerView.Adapter , RecyclerView.LayoutManager and ItemAnimator .
The function of the RecyclerView.Adapter is to "get" the data from any data source and "transform" it into views to be displayed by RecyclerView.
Managing the layout / layout on how this set of views is displayed is the responsibility of the RecyclerView.LayoutManager.
The ItemAnimator allows you to animate items when they are moved.
Delegating these responsibilities allows RecyclerView to display the same set of data in different ways. Not only how each item (field) of the item is displayed, but also the arrangement of each item in its set: vertical or horizontal list, uniform grid or stggered , or any other form. >
I think the name is because the implementation of RecyclerView.Adapter forces that views no longer used are reused when needed.
This is achieved with the methods onCreateViewHolder()
and onBindViewHolder()
.
- When a new set of views is required, the
onCreateViewHolder()
method is called followed by onBindViewHolder()
.
- If there are views available, only the
onBindViewHolder()
method is called.
On the other hand it requires the implementation of the "Default View Holder", where an object RecyclerView.ViewHolder is used to "save" references to views , avoiding repeated use of the findViewById()
method.
Note that both ListView and GridView make it possible to reuse views and implement the "Default View Holder", however its implementation is optional.
In several publications I have seen it be presented as a substitute for the ListView. In my opinion it's more than that: it's a new, more flexible, structured, and higher-performing approach to viewing a limited view of a large dataset.
- Flexible because it uses customizable external objects.
- Structured, because it explicitly requires the reuse of views and the implementation of the Default View Holder.
- Performance, plus the reuse of views and default View Holder, allows the selective item update , preventing the entire old dataset from being replaced by the new dataset. Only changed / deleted / inserted items are updated.
I use RecyclerView when I want to take advantage of what it offers differently: performance, layout and custom animations. In "simpler" cases, with little data and not subject to change, I use the ListView or GridView, but I always implement the ViewHolder pattern.
As a complement see:
Is there anything on other platforms that behaves similarly so I try to see better?