Understanding RecyclerView Part 1: The Basics

A beginner’s guide to recyclable lists on Android

NOTE: This article was first published on Context Neutral. Visit to find more related articles and guides.

1. What is RecyclerView?

RecyclerView is a list drawing library that essentially provides a fixed-size window to load a large dataset into. It recycles the views it created at the begining when the views go out of scope (window) and then if there is a need, reuses them to make it seem as if the views were never offloaded and were virtually present outside the window. See the three windows below? All three are having a RecyclerView in them. The first makes use StaggeredLayoutManager, while the second and the third make use of LinearLayoutManger. We will see what Layout Managers are later.

3 different layouts with different Layout Managers (2nd and 3rd actually have the same)
implementation 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'

2. How is it different from ListView?

RecyclerView can easily be called the better ListView. It works just like a ListView — displays a set of data on the screen but uses a different approach for the purpose. RecyclerView as its name suggests recycles Views once they get out of scope (screen) with the help of ViewHolder pattern. Sure, ListView can also have ViewHolders but that’s not available by default forcing us to add more code to improve list performance. On the other hand, ViewHolders are an important part of the whole RecyclerView architecture i.e. the system forces you to have a ViewHolder for any layout you may include. The architecture then uses these ViewHolders to create a View copy of the layout to be used, and instead of recreating them, uses the cached views instead and binds them with new data.

GridLayoutManager glm = new GridLayoutManager(context, 3);
RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator(); 

3. Definition and attributes

A RecyclerView is nothing but a simple ViewGroup that implements ScrollView. What makes RecyclerView magical is its ability to handle the Views inside it.

4. What is Layout Manager?

A layout manager is the heart and soul of a RecyclerView. It is responsible for letting the RecyclerView know when to recycle a child view once it’s gone out of scope. Without it, RecyclerView can’t figure out what kind of layout it is supposed to lay out on the screen, will it be a linear layout or will there be a grid with spans or better will this grid layout be a staggered layout that doesn’t respect the idea of keeping views in a row at the same height. The basic idea behind LayoutManager was to make it possible to dynamically change the layout of the same dataset with just an applicaton of a setter.

5. Recycler Adapter

The most important part of the whole RecyclerView architecture. A RecyclerView adapter just like any other adapter has the responsibility to bind datasets to Views that will be displayed in the window. A general adapter, essentially, is just an iterator that gets its limit from getCount() that generally depends on the size of the dataset and creates new views and binds data to them from that very dataset. ListView or ViewPager uses this kind of adapter. RecyclerView on the other hand uses a more complex system to bind data and handle views.

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// return new ViewHolder reference here based on the viewType
public void onBindViewHolder(RecyclerView.ViewHolder holder, int pos) {
if (holder instanceof MyViewHolder1) {
// bind data to the views of MyViewHolder1
public int getItemCount() {
if (dataset == null)
return 0;
return dataset.size();

6. Dynamically changing data (notify)

This actually isn’t any standalone feature of RecyclerView, instead it is done by the RecyclerView.Adapter as done by any other adapter used for displaying lists.

7. What now?

We just got to know about RecyclerView, and how it can easily be called the better ListView. We also got to know the key differences between ListView and RecyclerView and how RecyclerView excels at performance by recycling ViewHolders. Also, we now know the need of layout managers and how they can be used to change the look of the recycler layout as well as how adapter works internally to recycle, reuse views and bind data as per the view type.

Failing with experiments @ https://contextneutral.com

Failing with experiments @ https://contextneutral.com