RecyclerView : Why we need this and How it works internally ?

Yashchaturvedi
5 min readOct 5, 2023

--

Whenever we need to show some list of huge data set, we have options like ListView, GridView and RecyclerView. But most of the developers go with RecyclerView only.
So lets discuss issues with ListView/GridView and how RecyclerView solve those issue. Later we will discuss about internal working of RecyclerView.

Limitations with ListView/GridView :
1. Unefficient performace when dealing with huge list of items.
2. Consume more memory as it doesn’t reuse views.
3. One way scrolling orientation possible.
4. Complicate to use ViewHolder pattern in ListView
5. Having limited animations or need to implement custom animations

According to Android documentation:

RecyclerView is a flexible view for providing a limited window into a large data set. It is basically a new ViewGroup used to render any adapter-based view using ViewHolder pattern.

Components of RecyclerView:-

  • Data: It doesn’t matter where the data comes from. You can create the data locally, as you do in the practical, get it from a database on the device as you will do in a later practical, or pull it from the cloud.
  • Layout: All list items look the same, so you can use the same layout for all of them. The item layout has to be created separately from the Activity layout, so that one View item at a time can be created and filled with data.
  • A layout manager: The layout manager handles the organization (layout) of user interface components in a View. Each ViewGroup has a layout manager. For LinearLayout, the Android system handles the layout for you. RecyclerView requires an explicit layout manager to manage the arrangement of list items contained within it. This layout could be vertical, horizontal, or a grid. The layout manager is an instance of Recyclerview.LayoutManager to organize the layout of the items in the RecyclerView. It can be LinearLayoutManager, GridLayoutManager or StaggeredGridLayoutManager.
  • An adapter: Use an extension of RecyclerView.Adapter to connect your data to the RecyclerView. It prepares the data and how will be displayed in a ViewHolder. When the data changes, the adapter updates the contents of the respective list item view in the RecyclerView.
  • A ViewHolder: Use an extension of RecyclerView.ViewHolder to contain the information for displaying one View item using the item's layout.

Glossary of terms:

  • Adapter: A subclass of Adapter responsible for providing views that represent items in a data set.
  • Position: The position of a data item within an Adapter.
  • Index: The index of an attached child view as used in a call to getChildAt. Contrast with Position.
  • Binding: The process of preparing a child view to display data corresponding to a position within the adapter.
  • Recycle (view): A view previously used to display data for a specific adapter position may be placed in a cache for later reuse to display the same type of data again later. This can drastically improve performance by skipping initial layout inflation or construction.
  • Scrap (view): A child view that has entered into a temporarily detached state during layout. Scrap views may be reused without becoming fully detached from the parent RecyclerView, either unmodified if no rebinding is required or modified by the adapter if the view was considered dirty.
  • Dirty (view): A child view that must be rebound by the adapter before being displayed.

How RecyclerView works internally?

So lets discuss how recyclerview recycles the views internally.
Lets suppose initially item 1 to item 5 was visible to the screen. So RecylerView Adapter will create 7 items initially. 1 scrap view (item x)and 1 waiting view (item 6) initially. When we scroll one item above, item x moves up and a new item, item 6 becomes visible.

Now, item 7is in the waiting view.

Here item x has moved out from the visible view on top of the screen. This is called a scrapped view which will later used to create waiting view will be calling it as Recycled View.

Where these scrapped view stores?
Scrapped view get stored in the view cache. LayoutManager request view from RecyclerView and RecyclerView fetch view from the view cache.
If the scrapped view is available in the cache it will return view else recyclerview will create the new view.

So, if we have 4 different viewTypes, recyclerView will manage 4 different view cache or recycler pool.

IMPORTANT FUNCTIONS OF ADAPTER:

1. onCreateViewHolder() ->
This method is called when the ViewHolder is created. It initializes and inflates the view for the item in the RecyclerView. This view uses the item layout created earlier which displays text.

2.Overriding onBindViewHolder() ->
onBindViewHolder() is called with the ViewHolder and a “position,” which denotes the item’s position in the list that is being bound. This position can be used to extract the underlying data for the cell and pass that into the ViewHolder to bind the data to that holder’s UI.

3. Overriding getItemCount()
The RecyclerView displays a list, so it needs to know how many items are in the list. Since size of list.

Pro tip:-
1.
Since onBindViewHolder() get called everytime when new view get attached to the layout. It means doing some heavy task in onBindViewHolder() can cause lagging in scrolling. So we should avoid doing any heavy task inside this method. We should do heavy load tasks in onCreateViewHolder().

2. Use DiffUtil to avoid flickering of the screen while data re-rendering.

3. Use recyclerView.setHasFixedSize(true): By this method, you told RecyclerView to not calculate items size every time they added and removed from RecyclerView.

Conclusion:

ViewHolders and view recycling are the driving forces behind the enhanced performance of RecyclerView. When dealing with multiple view types, such as ViewType1 and ViewType2, the RecyclerView maintains two distinct pools of recycled views: one for ViewType1 and another for ViewType2.

As views are recycled, RecyclerView ensures that ViewType1 views are exclusively assigned to other ViewType1 views, and ViewType2 views are allocated only to ViewType2 views. This intrinsic mechanism is fundamental to RecyclerView’s internal workings and contributes to its overall efficiency.

For such interesting discussions, lets connect on LinkedIn.

If you found this article helpful, hit the 👏

--

--

Yashchaturvedi
Yashchaturvedi

Written by Yashchaturvedi

Experienced Android developer who is digging dive into the main components of android development. I am trying to build myself.

No responses yet