Should we use RecyclerView to replace ListView?

AndroidListviewAndroid Recyclerview

Android Problem Overview


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 ?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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.

Solution 3 - Android

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.

Solution 4 - Android

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.

Solution 5 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionXcihnegnView Question on Stackoverflow
Solution 1 - AndroidyigitView Answer on Stackoverflow
Solution 2 - AndroidPhantômaxxView Answer on Stackoverflow
Solution 3 - AndroidElletlarView Answer on Stackoverflow
Solution 4 - AndroidinmythView Answer on Stackoverflow
Solution 5 - AndroidgrantespoView Answer on Stackoverflow