Open In App

How RecyclerView Works Internally in Android?

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.



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

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.

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.

Article Tags :