Should we use RecyclerView to replace ListView? [closed]
Asked Answered
C

5

241

Android Docs say:

The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views. Use the RecyclerView widget when you have data collections whose elements change at runtime based on user action or network events

Actually ListView can do all of the above if efficiency doesn't matter, and we have found many issues when we use RecyclerView to replace ListView:

  1. There is no onItemClickListener() for list item selection - solution

  2. No divider between list items - solution

  3. No built-in overlap selector, there is no visual feedback when you click list item - solution

  4. No addHeaderView for list header - solution

Maybe more issues ...

So when we use RecyclerView to replace ListView, we have to do much extra coding to reach the same effect as ListView.

QUESTION:

  • Is it worth that we replace ListView with RecyclerView totally ?
  • if not then in which case should we better use RecyclerView instead ListView, and vice versa ?
Columbite answered 8/2, 2015 at 9:43 Comment(4)
your thread is just the first issue, and is not my questionColumbite
Just mention that you must use a recyclerview if you are interested in using a collapsed action bar. medium.com/android-bites/…Heroic
you should use recyclerview because it offers more control than listview. It is litte bit complex but you get there then your life will be super easy whenever you are dealing with list kind of thing.Fourwheeler
better is the enemy of good.Whisenhunt
N
125

If ListView works for you, there is no reason to migrate. If you are writing a new UI, you might be better off with RecyclerView.

RecyclerView is powerful when you need to customize your list or you want better animations. Those convenience methods in ListView caused a lot of trouble to people which is why RecyclerView provides a more flexible solution to them.

The major change you need to make for migration is in your adapter. If you want to keep calling notifyDataSetChanged, you lose most of the animation & binding benefits. But if you can change your adapter to dispatch detailed notify events (added/removed/moved/updated), then you get much better animations and performance. These events let RecyclerView choose correct animations and it also helps it avoid unnecessary onBind calls. You'll get a huge benefit if your item views are complex. Also, going forward, there will be more components around RecyclerView.

Nuno answered 8/2, 2015 at 19:40 Comment(4)
If I could give you 100 upvotes I would. The notifyDataSetChanged() method was causing my RecyclerView to keep asking for new ViewHolders all the time, gutting the benefits of the ViewHolder pattern. Looking for the solution I found your comment which answers my problem pretty much in passing :D thanks!Schoolman
I don't agree "You'll get a huge benefit if your item views are complex", I have recyclerView in which adapter has 3 if else, Recyclerview performs poorly in my case. I had to disable recycling using isRecyclable(false); and final result is highly laggy RecyclerView. I can't even change this, it will take lot of time to go back to listview :(Jermayne
people use RV w/ many more types then that w/o any problem and good performance. There is something else wrong in your code. You should use systrace/traceview and see whats going on.Nuno
@kashyapjimuliya as yigit mentioned, 3 if-else should not cause that. Firstly, you didn't mention where you put these if-else; secondly, disabling recycling should make it slower not faster. Why? Because inflating a view and doing findViewById() is a lot expensive than doing it the ViewHolder way.Strom
E
9

1 You can use an interface to provide a click listener. I use this technique with ListViews, too.
2 No divider: Simply add in your row a View with a width of match_parent and a height of 1dp and give it a background color.
3 Simply use a StateList selector for the row background.
4 addHeaderView can be avoided in ListViews, too: simply put the Header outside the View.

So, if efficiency is your concern, then yes, it's a good idea to replace a ListView with a RecyclerView.

Ereshkigal answered 8/2, 2015 at 10:27 Comment(1)
These are not my questions, I have had link for each issue solutionColumbite
A
7

I had until recently still been using ListView for very simple lists. For example if I want to display a simple list of text options...

I based that decision on 'human factors', that creating a simple ListView with less code is better if performance is immaterial. I often think of a professor in college that was fond of saying: "My teacher the great Niclaus Wirth, the inventor of Pascal, used to say if a program has more than 50 lines of code it is sure to be wrong..."

But what has convinced me to stop using ListView is that it has recently been moved into the "Legacy" category in the Android Studio design tool along with RelativeLayout.

https://developer.android.com/reference/android/widget/ListView

I think this is 'soft' form of 'deprecation'. It would be too disruptive if it was actually deprecated and all conscientious developers moved their code to RecyclerView.

Further, the intro to ListView warns right at the top that RecyclerView is a better option: "For a more modern, flexible, and performant approach to displaying lists, use RecyclerView."
https://developer.android.com/reference/android/widget/ListView

Also, the guide to ListView is still talking about cursor loaders, but then getSupportCursorLoader() itself has just been deprecated in API 28.
https://developer.android.com/guide/topics/ui/layout/listview

Recent enhancements to Android Studio:

File -> New -> Fragment -> Fragment (List)

This gives us a fully working RecylerView populated with basic text. That gets rid of my last real reason for using ListView because it is now just as easy to set up a basic RecylerView.

In summary, I don't intend to use ListView at all for new development because labelling it has 'legacy' is one step away from deprecating it.

Assist answered 25/7, 2018 at 17:31 Comment(0)
D
3

The only case when it's still fine to use ListView is when the list is static . For example: navigation.

For everything else use RecyclerView. Since RecyclerView deals with recycling it will be easier to do visual related things that were tightly coupled in ListView, like changing position / rearrangement, animation (in fact it comes with RecyclerView.ItemAnimator), custom layouts.

Also if you want to use CardView I believe it's the only way to go.

Daub answered 8/4, 2015 at 19:33 Comment(2)
Are you referring to grabbing a list of data from the servers, then stored in the sqlite on the phone to be used in a list display (of lets say your followers) it is better to use a recycler view than a listview with a view holder?Clarance
Yes. But to be precise I don't see any role for ListView anymore except navigation and that's because I think it's overkill to use RecyclerView there.Daub
C
2

A great alternative is using the BaseAdapter. It supports the use Viewholder pattern and mine contains 100+ rows with bitmaps and buttons and it runs very smooth.

Chiclayo answered 24/1, 2017 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.