Open In App

How RecyclerView Works Internally in Android?

Last Updated : 27 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

RecyclerView is a ViewGroup added to the android studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages. It has been created to make possible the construction of any lists with XML layouts as an item that can be customized vastly while improving the efficiency of ListViews and GridViews. This improvement is achieved by recycling the views which are out of the visibility of the user. For example, if a user scrolled down to a position where items 4 and 5 are visible; items 1, 2, and 3 would be cleared from the memory to reduce memory consumption. When developing an Android app, there is a good chance that we will need to use RecyclerView. In this article, we’ll look at how RecyclerView works in the Android operating system. We’ll talk about it.

  1. What Exactly is RecyclerView?
  2. RecyclerView Components
  3. How Does it Actually Work?
  4. Why ViewHolder is used in RecyclerView?

What Exactly is RecyclerView?

RecyclerView is a ViewGroup that populates a list based on a collection of data provided by ViewHolder and displays it to the user on-screen. You may refer to this article RecyclerView in Android with Example

RecyclerView Components

RecyclerView’s main components are,

  1. Adapter\ViewHolder\
  2. LayoutManager\Adapter
  3. It is a RecyclerView subtype.

The adapter is a class. It takes the data set that needs to be shown to the user in RecyclerView. It acts as the main responsible class for binding the views and displaying them. The majority of the work is done within the RecyclerView’s adapter class.

  • ViewHolder: ViewHolder is a type of helper class that allows us to draw the UI for individual items on the screen.
  • LayoutManager: LayoutManager in recyclerView assists us in determining how to display the items on the screen. It can be done either linearly or in a grid. RecyclerView comes with a few layoutManager implementations by default.

It functions as the RecyclerView’s governing body, informing the RecyclerView’s adapter when to create a new view.

How Does it Actually Work?

So, let’s take a look at how the recyclerView actually works. When people talk about recyclerView, they always say that it recycles the views. But what exactly does it mean? So, let’s say we’re scrolling through a list with 50 items in it and we only show 5 of them at a time, as in

RecyclerView in Android

Image 1: The recycler view.

Item 1 through 5 is the ones that are visible on the screen. When we scroll up, item x will be the one that appears next on the screen. Every item in this list is unique and is traditional!

So, how does recycling work in this?

Let’s take it one step at a time.

  • Step #1: At the initial launch, the first items x through 4 must be displayed on the screen. So those are the five items that are visible in the view mode. Let us refer to them as a visible view. And, when the list is scrolled up, item 5 is the new item that will be loaded.
  • Step #2: When we scroll up one item, item x moves up and a new item, item 5, enters the visible view category. Item 6 is now in waiting mode. Item x has moved out of the visible view at the top of the screen. This is known as a scrapped view.
  • Step #3: Let’s say we scrolled up one more step. This will remove item 1 from the screen and allow item 6 to enter. Item 1 is also scrapped in this case. We now have two deleted views, item x and item 1. They will now be saved in a scrapped views collection. So, when we need to load a new view in the visible view group, consider item 7, the view from the scrapped view collection is used.
  • Step #4: When we load item 7, we now select a view from the scrapped views collection. The view that we loaded from the scrapped view is referred to as a dirty view. This is how recycling of views occurs in recyclerView, which is one of the primary reasons for RecyclerView’s improved performance. The views of the item are reused in this process to draw new items on the screen.

Why ViewHolder is used in RecyclerView?

ViewHolders in RecyclerView helps to optimize the views in another way. So, let’s say we have 100 items on the list and want to show 5 of them on the screen. Each item in this list has one TextView and one ImageView. So, mapping each view using findViewById is always an expensive task, and imagine how many findViewByIds we’d need to map all of the TextViews and ImageViews of 100 items, i.e. 200 findViewByIds. So, when we use RecyclerView, only six items are created at first, with five loaded at once to be displayed on-screen and one to be loaded. We now have 7 ViewHolders if we scroll down the list. One for the scrapped view, one for the to-be-loaded view, and five for the ones that are displayed. As a result, we can only use 14 findViewByIds at a time because we have a maximum of 7 ViewHolders.

Conclusion

ViewHolders and view recycling have resulted in improved performance in RecyclerViews. Similarly, if we have multiple view types, say ViewType1 and ViewType2, we will have two distinct collections of scrapped views of the ViewType1 and ViewType2 types. And, when the views are recycled, the ViewType1 view will be assigned to only ViewType1 views, while ViewType2 views will be assigned to ViewType2 views. This is how recyclerView functions internally and effectively.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads