Understanding RecyclerView. A high-level Insight. Part -1.

Dhruvam Sharma
AndroidPub
Published in
5 min readDec 31, 2017
Source: Youtube

There are numerous articles on implementation of RecyclerView and CardView and what not. But there is not a single article or blog that states the high-level and the in-depth knowledge of the RecyclerView.
With this article I am trying to achieve this and help all the new developers in the RecyclerView World. This article is a part of a multi-part series for understanding RecyclerView.

Before we begin, I would love for you to try my latest gaming application that implements all of the features that I am going to talking in this series. Any feedback is appreciated. Open this link in your mobile, if possible.
Try it:
Gamezop

Gamezop — A Gaming Platform.

All the parts of the series:

  1. Part-1: Understanding RecyclerView - A high level insight.
  2. Part-2: Understanding Components of a RecyclerView.
  3. Part-3.1: RecyclerView Implementation.
  4. Part-3.2: RecyclerView Implementation.
  5. Part-4: ItemDecoration in RecyclerView.
  6. Part-5: Animations in RecyclerView.
  7. Part-6: Wrapping Up.

Let us start with the definition mentioned in Android Documentation.

A RecyclerView is a flexible view for providing a limited window into a large dataset.

Did you understand anything? At least I did not. So let’s begin understanding RecyclerView and then we’ll write our own definition. So what all do we need for RecyclerView Recipe?

  1. A basic history of the times when ListView ruled.
  2. What all characters of the ListView made us powerless.
  3. Realization of the need for something NEW.
  4. And we’re done.

A basic History of the times when ListView ruled.

ListView is a scrollable viewgroup (A special View that can contain other views) that helps in displaying data from a dataset in a vertical order, where each item is placed immediately below the previous view. The listview enjoyed a large amount of time on the throne for displaying a list. And Google hadn’t come up with a better option yet. It displayed similar data collection in each row and was helpful in creating list like these.

Ewww. Are these even Lists?

What all characters of the ListView made us powerless.

Firstly, without even going into details of the features that render ListView useless at certain points, I want you take a look at 3 lists shown above in the picture.
When you’re done feeling bad, let us start with the characters of lonely ListView .

  1. Only Vertical Scrolling: Android allows listviews to be scrollable only in verticle direction. No horizontal lists allowed. And also, If you want to use Grid as a ListView, you cannot! You need to use another widget, which is GridView. This is a putting off limitation of ListView.
  2. Lagged Scrolling: Lists rendered using ListView widget have low performance. It provides us with a scrolling list that lags too much. This is because ListView has a habit of creating as many views (rows) as there are data items in the dataset. This creation of views and using findViewById() method is a costly affair. (Sorry for including a method in the high-level-view but I couldn’t resist) And the reason why this is lagged and how it was overcome, will be done in the remaining parts to come.
  3. Animations: ListView had no in-built features for providing animations. It took days for animations to be incorporated into them.

There are many more factors that render ListView powerless for us. But I guess, these factors are enough to decide to switch for something NEW.

Realization of the need for something NEW.

These factors made us (at least me) to switch from ListView to RecyclerView. Now we are done with ListViews and why they are bad for our health. Let’s concentrate on WHAT, WHY and WHEN of RecyclerView.

RecyclerView with easy animations. Source: Medium Article

I told you we’ll make our own definition of RecyclerView. Now let’s get to it.

A RecyclerView is a new ViewGroup used to render any adapter-based view in horizontal or vertical or grid or staggered grid manner.

We’ll break down the definition and understand what all there is.

  1. ViewGroup: It is a special kind of View that can contain other Views.
  2. Adapter-Based View: It is a view which is handled by any adapter.
  3. Adapter: It is a dumb object (just like me) that handles the View Creation and data binding to the View.
  4. Horizontal, Vertical, Staggered Grid or Grid: These are the layouts for rendering lists.

The recyclerview widget was added in the design support library in version 22.1.0 It came packed with the Material Design.

A high level Understanding of The Working

Let us understand the first picture that we saw at the beginning of this article.

The yellow boxes are the views or the rows in the RecyclerView. Let’s suppose there are 30 items in the array, or database or any other dataset and in the visible part of the screen (the viewport) you can see only 4 rows or views at a time. A ListView would normally create 30 views, one for each data item and then bind the data to it. This creation of Views is a CPU-intensive task. And a normal listView would lag as it usually happens.

What RecyclerView does is, it creates only 4 Views that will be first displayed on the screen and whenever we start scrolling, it creates sufficient more views (In this example, It will create in total of 8 views (4+4)) and the views that go out of visible area are stored . So the number of CPU Cycles are reduced as views are reduced from 30 to 8. The Views which are kept for recycling are known as scraped views.

When to use a RecyclerView?

The answer is always. But okay, I mean, you need them when-

  1. Whenever you need a list of items.
  2. Whenever these items are not similar. Example, one row may contain 3 TextViews and the other row contains5 ImageViews.
  3. Whenever the elements might change at runtime, due to user click events or network events.
  4. There are more reasons too. Obviously!

Now we are done with the first part of the series. We’ll meet again for the next parts.

This is my first Technical Article. Please clap and comment and help me with the errors. And I apologize for any grammatical errors in advance, if any.

--

--

Dhruvam Sharma
AndroidPub

Google-certified Android Developer @Unikon. Android Geek. In love with Flutter. Blockchain Explorer. Dancer. 🕺 Reader. Coffee Addict. 😍