As an Android developer, Many people asked me what’s the difference between ListView and RecyclerView. In fact I myself was not aware of the differences unless I dig deep down and explored more.
Based on my experience of using both the components, I would like to place here few points which might help you understanding in a better way.
With the advent of Android Lollipop, the RecyclerView made its way officially. The RecyclerView is much more powerful, flexible and a major enhancement over ListView.
As per Android developer site, RecyclerView added in version 22.1.0 and it belongs to Maven artifact com.android.support:recyclerview-v7:28.0.0-alpha1.
The ViewHolder pattern allows us to make our list scrolling act smoothly. It stores list row views references and, thanks to this, calling the findViewById()
method only occurs a couple of times, rather than for the entire dataset and on each bind view.
The RecyclerView’s adapter forces us to use the ViewHolder pattern. The creating part (inflating the layout and finding views) and updating the views are split into two methods — onCreateViewHolder()
and onBindViewHolder()
.
The ListView, on the other hand, doesn’t give us that kind of protection by default, so without implementing the ViewHolder pattern inside the getView()
method, we’ll end with inefficient scrolling in our list.
2. LayoutManager
The LayoutManager takes responsibility for layouting row views. Thanks to this, RecyclerView doesn’t have to think about how to position the row view. This class allows us to choose the way that we want to show the row views and how to scroll the list. For example, if we want to scroll our list vertically or horizontally, we can choose LinearLayoutManager. For grids, it is more suitable to choose GridLayoutManager.
Previously, with the use of the ListView, we were only able to create a vertical-scrolling list, so it wasn’t that flexible. If we wanted grids on our list, we had to choose the other widget for that — GridView.
3. ItemDecoration
Duty of the ItemDecoration is simple in theory – add some decorations for the list row views – but in practice, it’s that simple to implement if we want to create a custom one. In this case, we should extend the ItemDecoration class and implement our solution. For example, the RecyclerView list has no dividers between rows by default and it’s consistent with the Material Design guidelines. However, if we want to add a divider for some reason, we can use DividerItemDecoration and add it to the RecyclerView.
In case we use the ListView, we have to figure out rows decorations by ourselves. There is no helper class like ItemDecoration for this widget.
4. ItemAnimator
The last but not least component of RecyclerView that I want to mention is ItemAnimator. As we can expect, it’s handling row views animations like list appearance and disappearance, adding or removing particular views and so on.
By default, RecyclerView’s list animations are nice and smooth. Of course, we can change that by creating our own ItemAnimator, which is also not that easy. To make it easier, we should extend the SimpleItemAnimator class and implement the methods that we need (just add animations to a ViewHolder’s views).
To be honest, implementing animations on the ListView was a pain in the ass. Again, we had to figure out how we want to handle them. Nothing nice. I’m glad it’s gone.
5. Notifying adapter
We have a couple of cool notifiers on the RecyclerView’s adapter. We are still able to use notifyDataSetChanged()
but there are also ones for particular list elements, like notifyItemInserted()
, notifyItemRemoved()
or even notifyItemChanged()
and more. We should use the most appropriate ones for what is happening, so the proper animations will fire correctly.
Using ListView, we were able to use just notifyDataSetChanged()
on the adapter and then had to handle the rest ourselves, again.
Since we already use the RecyclerView, it would be good to make our working with this tool more easy and enjoyable. Here is a couple of helpful features.
1. DiffUtil.Callback
The callback helps us to skip manual handling of notifying an adapter. Instead of using different methods for each type of notifications as notifyItemInserted()
, notifyItemRemoved()
or notifyItemChanged()
we just can use this simple method:
But even this approach is not the end. 🙂
2. ListAdapter
ListAdapter
is a casing on a RecyclerView. Adapter with the full power of DiffUtil
. Thanks to ListAdapter
we don’t need:
- keeping field with the list of items
- handling items animations
- writing methods for rendering new items
- caring for the size of items list
How does it look like? We need to define three things: two methods onCreateViewHolder
, onBindViewHolder
and DiffUtil.ItemCallback
(the simplest version of DiffUtil.Callback
).
Sample Code:
For a rendering of new items, it’s enough to use adapter.submitList(newItems)
method of ListAdapter
.
Working with RecyclerView has never been so simple and enjoyable.
ListView vs RecyclerView – wrap up
The ListView served us for a long time. We were able to cover most of the cases. But the user’s needs are different now and they are much more challenging. List designs became more and more complex and the ListView wasn’t helping with handling them. Material Design brought other changes, 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, layouting it’s easier to use and the API is much nicer. So, if you ever wonder which one you should choose, your first thought should be the RecyclerView. I hope this article helped you to understand what is the difference between ListView and RecyclerView.
Or use the second steps:-