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 a ListView styled RecyclerView use the LinearLayoutManager

For a GridView styled RecyclerView use the GridLayoutManager

For a staggered grid effect, use the StaggeredGridLayoutManager

For a custom layout manager, extend the RecyclerView.LayoutManager class

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.