1

I was reading about the difference b/w recyclerview and listview and found out that recyclerview is faster than listview.

I tried to search online but not found any satisfactory answer I know it is used ViewHolder pattern and Notifying adapter but what does it does intearlly so it is faster?

Harsh Shah
  • 2,162
  • 2
  • 19
  • 39
  • check this link : https://stackoverflow.com/questions/28525112/android-recyclerview-vs-listview-with-viewholder/31199564#31199564 – YuvrajsinhJadeja Oct 01 '19 at 09:33
  • The link is a good summary of what RecyclerView offers over ListView. But from the point of view of performance, RecyclerView just allows devs to make fewer performance impacting mistakes than ListView since it formalises a lot of the boilerplate that we use to do with ListView to make it fast and efficient (e.g. ViewHolder pattern). – Ryujin Oct 01 '19 at 09:40

3 Answers3

2

There are some differences between these two views.

ListView is a bit heavy and it has a lot of responsibilities. Whenever we have to handle the list, such as to configure it in some way, the only way to do this is through the ListView object or inside the adapter.

A lot of bad things in the ListView were fixed or changed in the RecyclerView. It’s more efficient by default, the layout is separated and we have more possibilities over the data set inside the adapter.

These are some crucial differences between ListView and RecyclerView:

1 ViewHolder

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 is 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 gives us the opportunity 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 the 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

A 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. Again, we had to figure out how we want to handle them.

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 actually 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.

majid ghafouri
  • 787
  • 2
  • 7
  • 23
1

Recycler View you could say is an efficient way to create list of views.

If you have 1000 items like ur contact list , and If ur visible screen can show only 10 items at once, it will Create only 10+1 (or +2) Views and as u scroll , items/views that left will be reused (not create) to show new data.

Recycler View by default does this, where as List View by default doesn't do.

vizsatiz
  • 1,933
  • 1
  • 17
  • 36
0

Because of ViewHolder Pattern.

Thats was the simplest answer. Now for some details.

What recycler view does is what it's name indicates "Recycle", yes it recycles items, and it does it with the help of ViewHolder Pattern.

By Using ViewHolder we do-not need to call findViewByID() every time we go through getView()method. The reference for all rows are stored in-memory. This increases the performance significantly, as findViewByID()is a heavy process.

Hope this clears your confusion.

Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26