The Good, the Bad and the Ugly Things About the New RecyclerView

Wolox Engineering
Wolox
Published in
4 min readApr 23, 2015

What it is and what it is not

If you are an Android developer, no doubt you know that ListView is one of the most popular Android widgets. Its functionality and usage has no equal amongst its peers… until now.

Android Lollipop (API 21) introduced the new RecyclerView widget, with the intention of increasing ListView functionalities and taking them to the next level. Bear in mind that what Google purposely did with RecyclerView was to extend rather than to replace ListView functionalities and responsibilities. So, if you want to update your old and reliable ListView with the new state-of-the-art RecyclerView, you should know that it is possible, but not without some gotchas in the middle.

There is only one word that describes RecyclerView perfectly: flexibility. By decoupling the data from its visual representation, this widget can be used for many different purposes and provides the developer with a powerful tool.

Components

Almost vintage retrocompatibility

RecyclerView has super retrocompatibility. This is because it is part of Android’s v7 Support Libraries. This means that any device running on API 7 or higher will be able to use RecyclerView right away.

The easiest way to get RecyclerView in your project is by using Gradle:

And on your XML layout:

Boost your app with the ViewHolder pattern

RecyclerView forces you to use the ViewHolder pattern. To cut a long story short, this pattern holds references to views to avoid UI lookups as much as possible. In other words, instead of calling the method findViewById(…) each time you need to modify something from the UI, you just need to execute this method once and then save a reference to its result for future usage.

This results in a greater memory consumption (remember that you are using memory to keep the view’s references), but you also get a general improvement in the performance by not executing UI lookups anymore, which is a relatively expensive method (it is way faster to access an instance variable instead). This is where RecyclerView got its name from: recycling (reusing) view’s references.

However, don’t expect any miracles from the ViewHolder pattern. It does, indeed, give a performance boost to your app, but it will be much more evident on older (or low-end) devices. Generally speaking, powerful devices usually work fine with both ListViews and RecyclerViews.

Important: the ViewHolder pattern can also be used with ListViews and its performance impact will be the same. The only difference is that if you use RecyclerView, you must implement this pattern, while in ListViews this is an optional, but recommended technique.

Be flexible like chewing gum with LayoutManagers

The first big difference that we found in RecyclerView is that even though its purpose in life is to represent a given dataset graphically (just like any ListView), it does not necessarily need to be in a list of rows. RecyclerView has some tools (called LayoutManagers) that will help you achieve not only a list of rows, but also, grids and staggered grids of views.

For instance:

Even with LayoutManagers, there will be cases where you need to have more than one layout per item at the same time. Check out Part II of this post to learn how to manage these situations efficiently.

One Adapter to rule them all

Instead of having different adapters according to where the dataset comes from (like ArrayAdapter or CursorAdapter in ListView), RecyclerView uses a universal adapter implemented by you to handle the data sources. This reflects RecyclerView’s flexibility once again.

Make sure to extend RecyclerView.Adapter<RecyclerView.ViewHolder> and implement the following methods according to your views and dataset:

Pixar grade animations

If you are looking for a way to make the latest Toy Story animation movie then RecyclerView may not be your first choice. But, if you want to animate items such as adding, removing or swiping gestures, then RecyclerView may be useful for you.

The RecyclerView widget has out of the box support for custom animations by extending the ItemAnimator class.

A fade animation is applied by default. Besides, you can explicitly do this with:

mRecyclerView.setItemAnimator(new DefaultItemAnimator());

If you are looking for some already prepared animations for your RecyclerView you can use this library by Gabriele Marrioti.

Check out the second part of the post to learn some click listeners, layouts and performance tips and tricks!

Click here to read Part 2

Posted by Juan Ignacio Molina (juan.molina@wolox.com.ar)

www.wolox.com.ar

--

--