Ticker

6/recent/ticker-posts

Why RecyclerView? What is the difference between the ListView and RecyclerView in Android?

In Android, we can create a scrolling list either using a listView or a recyclerView. So in this blog, we’re going to discuss 5 major differences between the listView and recyclerView which every android developer should know.




Before going to discuss the differences between ListView and RecyclerView, let’s see what are ListView and RecyclerView?

ListView and RecyclerView – a brief history

As Android Developers, we can perform a scrolling list in a couple of ways, which mostly depends on what we need to do. The most popular methods are to use a ListView or a Recycler View.

Until Android Lollipop release, we mostly used the ListView widget in most of the applications and it wasn’t that bad. But unfortunately, it allows us to create just the row-scrolling list of elements and, to make that list scrolling go smoothly, we have to remember to do it correctly. Whenever we have to handle the listView, the only way to handle it through the ListView object itself or inside the adapter class, which make the ListView class bit too heavy.

But now we use the RecyclerView, which was introduced with the Android Lollipop and it proved to be a game-changer. It fixed the lot of problems that we faced in the ListView. By default, it is more efficient, the layout is separated and, we have more chances over the data set inside the adapter.

If you want to know more about RecyclerView, you can take a look at my article which shows how to implement a RecyclerView.

Now, let’s discuss the differences between ListView and RecyclerView.

ListView vs RecyclerView – differences

1.      ViewHolder

ViewHolder allows us to make our list scrolling act smoothly. It stores list row views references and, calling the findViewById() the method only occurs a couple of times, rather than for the entire dataset and on each bind view. 
 
The RecyclerView’s adapter makes us use the ViewHolder method. The creating part (inflating the layout and finding views) and updating the views divided into two methods — onCreateViewHolder() and onBindViewHolder(). 
 
On the other hand, the listview doesn’t give us that kind of functionalities, so without using the ViewHolder method inside the getView() method we’ll end up with inefficient scrolling list.


2.      LayoutManager

The LayoutManager takes responsibility for arranging the row views. So, RecyclerView doesn’t have to think about how to place the row views. The LayoutManager class allows us to choose the way that we want to place 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. 
 
Earlier, with the use of the ListView, we were only able to create a vertical-scrolling list, so it wasn’t that flexible. For grids on our list, we had to choose the GridView widget for that.


3.      ItemDecoration

The ItemDecoration is used to add some decorations for the list row views. If we want to create custom views, we should extend the ItemDecoration class and implement our solution. 
For example, if we want to add the divider between the rows of the recyclerView, we need to use the DividerItemDecoration and add it to the RecyclerView. 
 
But in the ListView, we have to figure out rows of decorations by ourselves. There is no helper class like ItemDecoration for this widget.


4.      ItemAnimator

The ItemAnimator is used to handling row views animations like list appearance and disappearance, adding or removing particular views, and so on. 
 
RecyclerView ItemAnimator is smooth and nice. We can change it by creating our own ItemAnimator, which is also not easy. To use it, we should extend the SimpleItemAnimator class and implement the methods that we need (just add animations to a ViewHolder’s views). 
 
But in the case of ListView, we need to figure it out by ourselves and it is not easy.


5.      Notifying adapter

We have many 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. 
 
Using ListView, we were able to use just notifyDataSetChanged() on the adapter and then had to handle the rest ourselves, again.
 


Wrap up

ListView helped us for a long time. We were able to reach most of the problems. But the user’s requirements are different now, and they are much more challenging. List designs became more and more complicated, and the ListView wasn’t helping with handling them.

Luckily, the RecyclerView was introduced and a lot of problems were solved. It’s more efficient by default, its animations are simpler, arranging the layout is becoming easier, and the API is much nicer. So, if you ever question which one you should choose, your first consideration should be the RecyclerView. I hope this article helped you to understand what is the difference between ListView and RecyclerView


Post a Comment

2 Comments

Please do not enter any types of spam links in the comment section.