What is a RecyclerView and How it works internally?
Wonder how identical horizontal/vertical list views populate in Android like how WhatsApp shows the chats/contacts list? Well, it can all be done using RecyclerView
. RecyclerView
is the ViewGroup
that contains the views corresponding to your data.
RecyclerView
itself is a view added to the parent layout that holds another(card/item) layout using an adapter. Each element in the list is defined by a ViewHolder
object.
RecyclerView is made by improving the efficiency of GridView
and ListView
. As from the name RecyclerView
, it recycles the views which are out of visibility.
Components of RecyclerView
Adapter
An Adapter acts as a bridge between an AdapterView
and the underlying data for that view. The adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.
Our Adapter class should extend Recycler.Adapter
class and override below methods for the recycler view to work
onCreateViewHolder
method prepares appropriate views for the individual data in the list.onBindViewHolder
method takes individual item position and holder as parameters and every visible item is filled with correct values.getItemCount
method returns the size of the input array
Every time when the user scrolls a recycler view, onBindViewHolder
is called with the visible position and fills the layout accordingly.
ViewHolder
ViewHolder
is a helper class for drawing the UI for individual items in the list.
It extends RecyclerView.ViewHolder
class. All the UI bindings happen in this 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.
Generally, LinearLayoutManger is used as LayoutManager for RecyclerView. We can determine the horizontal/vertical scrolling by assigning the specific LinearLayoutManager constants like LinearLayoutManager.HORIZONTAL
,LinearLayoutManager.VERTICAL
, etc.
How it works internally?
As from the name “RecyclerView”, we know that this view recycles the views attached to it. But how does the recycling of views work? Let’s see.
Let’s say we have a list of 50 objects which are needed to be populated through RecyclerView. We will be giving all the 50 objects to the Adapter and it prepares the view based on the object. But, we can see only 5 cards at one time from a normal screen device.
Here, Item-1 to Item-5 are the ones that are visible on the screen. Item 6
is the one that will be loaded next on the screen when we scroll up.
All the items here have their own instance of ViewHolder and the ViewHolder here is helpful for caching the specific item’s view.
So, how recycling works here?
- Item 1 to Item 5 are initially loaded on the screen and Item 6 is which needs to be visible when the user scrolls up which is called as Waiting view.
- When the user scrolls up Item 6 is visible and Item 1 goes under the top of the screen, disappears and flushes out. Item 1 is now called Dirty View.
These steps are executed simultaneously. - When the user again scrolls down Item 6 gets out of visibility and flushes out and Item 1 is loaded gets onto the screen.
This happens on every scroll by the user and the view visibility changes based on the currently visible items on the screen.
ScrapView is the view in RecyclerView which was once visible and now is not visible on the phone’s screen to the user.
The views which we take from scrap view collection and then after re-bound happens by the recyclerView adapter before it is drawn to the screen are called dirty views.
Reuse issues with RecyclerView
There will be few reuse issue when we are dealing with RecyclerView as it reuses the other view. So, make sure that we need to clean the views on onBindViewHolder method.
Example: Let’s take a ImageView
which we need to load an image from URL using Glide. As Glide is a image loading and caching library.
When same ImageView
is used by two or three elements, previously loaded image is populated(due to caching) on the newly loaded ImageView
. This raises an reuse issue.
To avoid this set ImageView
to null or set any default image in onBindViewHolder
method and then populate the image from URL once it is ready.
Conclusion
RecyclerView
got improved using ViewHolder
and recycling of views. We can have multiple ViewHolders
for the same RecyclerView
. We can specify the respective ViewHolder based on the element type.