Recycle ♻️ Everything!

Siddharth Jaswal 🎅
Founding Ithaka
Published in
3 min readMar 31, 2018

Lists are some of the most commonly used components in android applications. Most of the information is shown in vertically or horizontally scrolling lists. Since API 1, ListView has been the only way for showing items in scrolling lists. Until Android Lollipop’s release, we mostly used ListView and the API is intuitive. Unfortunately, it allows us to create just the vertical-scrolling list of elements and, to make the lists scroll smoothly, some additional effort needs to be put in. Then, during the Google IO 2014, a new player entered the field — the RecyclerView.

What is RecyclerView?

The RecyclerView was introduced with Android 5.0 (Lollipop). It is included in the Support Library. Thus, it is compatible all the way till Android API Level 7. RecyclerView’s main idea is to provide listing functionality in a performance friendly manner. It can actually recycle (as the name suggests) the items which it is currently working with, thanks to a pattern called View Holder. ViewHolders are caches of your View objects.

RecyclerView has less responsibility than what ListView had. In theory, the widget is simply a container that encapsulates a LayoutManager and an ItemAnimator, communicating with an Adapter, more precisely, a RecyclerView.Adapter.

Life with View Holders

In ListView, defining view holders was a suggested approach for keeping references for views. But it was not a compulsion. This major drawback of not using view holders could lead to a heavy operation of finding views by ids every time. Which resulted in laggy ListViews.

This problem is solved in RecylerView by the use of RecyclerView.ViewHolder class. This is one of the major differences in RecyclerView and ListView. When implementing a RecyclerView this class is used to define a ViewHolder object which is used by the adapter to bind ViewHolder with a position. Another point to be noted here, is that while implementing the adapter for RecyclerView, providing a ViewHolder is compulsory. This upgrade from the typical findViewById() to View Holders enables smoother scrolling through the list.

Layout Manager

Unlike ListView, RecyclerView does not have the responsibility to position the elements of the list. These are provided by LayoutManagers.

A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.

This separation of concerns allows the list widget to be used for different types of layout arrangements, for example, for vertical lists of items or even for displaying a grid layout.

You can use three types of LayoutManagers:

  • LinearLayoutManager: for the construction of vertical or horizontal lists;
  • GridLayoutManager: for the constructions of grids;
  • StaggeredGridLayoutManager: for the construction of grids whose items don’t have a fixed size, creating a type of mosaic.
Vertical List, Grid View, Staggered View, Mixed View (from left to right)

Item Animator

Animations in a list is a whole new dimension, which has endless possibilities. In a ListView, as such there are no special provisions through which one can animate theaddition or deletion of items.

On the other hand comparing Android RecyclerView vs ListView, it has RecyclerView.ItemAnimator class for handling animations. Through this class custom animations can be defined for item addition, deletion and move events. For simplicity, the RecyclerView already has some default animations, in case you don’t need any customizations.

Wrap Up

ListView served us for a long time. We were able to cover most of the cases. But user’s needs are different now and they are much more challenging. List designs became more and more complex and ListView wasn’t helping with handling them. Material Design brought other changes like material animations, too — it’s beautiful but complex.

Fortunately, the RecyclerView was introduced and a lot of problems were solved. It’s more efficient by default, its animations are simpler, layouts are easier to use and the API is better. So, if you ever wonder which one you should choose, your first thought should definitely be RecyclerView.

--

--