Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should we use RecyclerView to replace ListView? [closed]

People also ask

Should I use ListView or RecyclerView?

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.

What can I use instead of list view?

RecyclerView is a somewhat new view that came to substitute the ListView and GridView. From its documentation, you can see that it is a more efficient and advanced widget when compared to its predecessors, despite having many simplifications to support better animations and better arrangements of elements.

What is the benefit of using a RecyclerView?

The major advantage of the usage of RecyclerView is the performance when loading list items in the list to the view. RecyclerView prepares the view behind and ahead beyond the visible entries. It gives significant performance when you need to fetch the bitmap image in your list from a background task.

What is RecyclerView and when is it normally used?

The RecyclerView is a ViewGroup that renders any adapter-based view in a similar way. It is supposed to be the successor of ListView and GridView. One of the reasons is that RecyclerView has a more extensible framework, especially since it provides the ability to implement both horizontal and vertical layouts.


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.


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.