Hello Readers, CoolMonkTechie heartily welcomes you in this article.
In this article, we will learn about how recyclerView actually works in the Android system.We will focus the below points to understand the Android RecyclerView Working Concepts:
- What is RecyclerView?
- What is the Building components of RecyclerView?
- What is the benefits of RecyclerView ?
- What pattern is used in RecyclerView?
- How it actually works?
- How does RecyclerView optimize the view?
A famous quote about learning is :
” Tell me and I forget, teach me and I may remember, involve me and I learn.“
So Let’s begin one by one.
Displaying a list or grid of data is one of the most common UI tasks in Android. Lists vary from simple to very complex. A list of text views might show simple data, such as a shopping list. A complex list, such as an annotated list of vacation destinations, might show the user many details inside a scrolling grid with headers.
To support all these use cases, Android provides the
RecyclerView widget. So what’s next.
What is RecyclerView?
“RecyclerView is a ViewGroup, which populates a list on a collection of data provided with the help of ViewHolder and draws it to the user on-screen.“
What is the Building components of RecyclerView?
The major components of RecyclerView are:
It is a subtype of RecyclerView.Adapter class. It takes the data set which has to be displayed to the user in RecyclerView. It is like the main responsible class to bind the views and display it.
Most of the tasks happen inside the adapter class of the recyclerView.
ViewHolder is a type of a helper class that helps us to draw the UI for individual items that we want to draw on the screen.
All the binding of Views of the individual items happens in this class. It is a subclass of RecyclerView.ViewHolder class.
LayoutManager in recyclerView helps us to figure out how we need to display the items on the screen. It can be linearly or in a grid. RecyclerView provides by default a few implementations of layoutManager out of the box.
It is like the governing body of recyclerView which tells the recyclerView’s adapter when to create a new view.
What is the benefits of RecyclerView ?
The greatest benefit of
RecyclerView is that it is very efficient for large lists:
- By default,
RecyclerViewonly does work to process or draw items that are currently visible on the screen. For example, if your list has a thousand elements but only 10 elements are visible,
RecyclerViewdoes only enough work to draw 10 items on the screen. When the user scrolls,
RecyclerViewfigures out what new items should be on the screen and does just enough work to display those items.
- When an item scrolls off the screen, the item’s views are recycled. That means the item is filled with new content that scrolls onto the screen. This
RecyclerViewbehavior saves a lot of processing time and helps lists scroll fluidly.
- When an item changes, instead of redrawing the entire list,
RecyclerViewcan update that one item. This is a huge efficiency gain when displaying lists of complex items!
In the sequence shown below, you can see that one view has been filled with data,
ABC. After that view scrolls off the screen,
RecyclerView reuses the view for new data,
What pattern is used in RecyclerView?
RecyclerView uses Adapter pattern to display complex lists in the UI Screen after transform app data.
If you ever travel between countries that use different electric sockets, you probably know how you can plug your devices into outlets by using an adapter. The adapter lets you convert one type of plug to another, which is really converting one interface into another.
The adapter pattern in software engineering helps an object to work with another API.
RecyclerView uses an adapter to transform app data into something the
RecyclerView can display, without changing how the app stores and processes the data.
For the sleep-tracker app, you build an adapter that adapts data from the
Room database into something that
RecyclerView knows how to display, without changing the
How it actually works?
So, now we are going to discuss how the recyclerView actually works. When we talk about recyclerView, you would always hear someone saying that it recycles the views. But what does it actually mean?
So, let’s say when we are scrolling the list which has 50 items in the collection and we show only 5 items at once in the list.
Here, item 1 to item 5 is the ones that are visible on the screen.
item x 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.
This is how recycling of views happens in recyclerView which is one of the main reasons for the better improvement of recyclerView. In this process, the views of the item are reused to draw new items on the screen.
Similarly, if we have multiple view types, let’s say ViewType1 and ViewType2 then we would have two different collections of scrapped view of type ViewType1 and ViewType2.
And while recycling the views, the ViewType1 view will be allocated to only views of ViewType1 and ViewType2 views will be allocated to ViewType2 views.
This is how recyclerView works internally and efficiently.
How does RecyclerView optimize the view?
RecyclerView optimizes the view using ViewHolders.
So, let us say we have 100 items be displayed on the list and we want to show 5 items on the screen.
Each item here has 1 TextView and 1 ImageView in the item.
So, to map each view using findViewById is always an expensive task and just imagine the number of findViewByIds we might need to map all the TextViews and ImageViews of 100 items i.e 200 findViewByIds.
So, when we are using RecyclerView, then only 6 items are created initially with 5 loaded at once to be displayed on-screen and one is the one to be loaded.
Now, if we scroll the list then we have 7 viewHolders. One each for scrapped view and to be loaded view and 5 for the ones which are displayed.
So, at a time, maximum findViewByIds that we are using are only 14 as we have a maximum of 7 ViewHolders.
Because of ViewHolders and recycling of views, we have got the performance improvement in RecyclerViews.
That’s all about in this article.
In this article, We understood how recyclerView actually works in the Android system. We saw RecyclerView Concepts, Building Blocks , Benefits, Used Patterns, Internal working process and the reasons behind view optimization and performance improvement in RecyclerView.
Thanks for reading ! I hope you enjoyed and learned about Android RecyclerView Concepts. Reading is one thing, but the only way to master it is to do it yourself.
Please follow and subscribe us on this blog and and support us in any way possible. Also like and share the article with others for spread valuable knowledge.
If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.
Thanks again Reading. HAPPY READING !!😊😊😊