Recycler view advantages

React Native Introduction
June 23, 2018
One framework. Mobile & desktop
June 21, 2018
The RecyclerView was introduced with Android 5.0 (Lollipop). Of course, it is included in the Support Library. Thus, it is compatible event with Android API Level 7.

Similarly to the ListView, the RecyclerView’s main idea is to provide listing functionality. But as you know, this is done in a performance friendly manner. The ‘Recycler’ part of this view’s name is not there by coincidence. The RecyclerView can actually recycle the items with which it’s currently working. Surprise! Surprise! The recycling process is done thanks to a pattern called View Holder. By using it, there is no need to call findViewById() each time we go through the getView() method of our adapter. The references to all views for each list row are kept in-memory. This significantly increases the performance. Of course, you can use this pattern together with a ListView, but it is optional. If you want to use a RecyclerView, you have no other option, but to implement the View Holder pattern. It is mandatory.

Also, the RecyclerView does less things than the ListView. It’s always better to do less, but do it right. That sounds like the RecyclerView has less features than the ListView. But this is not true. It’s actually more flexible and more feature rich. Just all these options are distributed into different entities like the LayoutManager and ItemAnimator. Have you ever heard something about ‘Separation of concerns’? If not, check the Wiki page in the ‘See also’ section.

Another great thing about the RecyclerView is the way it interacts with LayoutManager and ItemAnimator. These two provide some useful functionalities.Thanks to the LayoutManager we can create lists with vertical and horizontal scrolling (LinearLayoutManager) and uniform and staggered grids (GridLayoutManager, StaggeredLayoutManager). Or we can create our own manager with extra bit of customization. Thanks to the ItemAnimator we can make use of different animations. There are animations for adding, updating and removing items from the RecyclerView.

And let’s not forget the great integration with the DiffUtil class. Long story short, this class helps us to calculate the difference between two lists of data. It’s a must when using the RecyclerView! You can check the article about the DiffUtil class in the ‘See also’ section.

Of course, there are some drawbacks when using the RecyclerView. The distribution of responsibilities to the LayoutManager and the ItemAnimator, adds extra complexity. And if we include the mandatory usage of the ViewHolder pattern, the learning curve becomes not so smooth. Our brains are not capable of working with many different components at the same time. Thus it can be harder to get used to the way of using the RecyclerView.

Another inconvenient thing is the lack of a listener like the OnItemClickListener. Of course, we can easily overcome that by using simple interfaces, but it’s still worth mentioning.

Pros & Cons of RecyclerView



Pros:

integrated animations for adding, updating and removing items
enforces the recycling of views by using the ViewHolder pattern
supports both grids and lists
supports vertical and horizontal scrolling
can be used together with DiffUtil


Cons:

adds complexity
no OnItemClickListener