Should we use RecyclerView to replace ListView? [closed] Should we use RecyclerView to replace ListView? [closed] android android

Should we use RecyclerView to replace ListView? [closed]


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.


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.


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.