Should I change the existing Listview in my app to RecyclerView?

谁都会走 提交于 2019-12-05 14:34:02

问题


Is there any advantages of converting the existing Listview in my app to RecyclerView?? Also when should I be using RecyclerView??


回答1:


That's a tough question to answer. First of all: Think of the RecyclerView as the successor of the AdapterViews which comes with alot of useful improvement especially in the fields of animations for each item. Actually that's also what the Docs say:

RecyclerView is a more advanced and flexible version of ListView. This widget is a container for large sets of views that can be recycled and scrolled very efficiently. Use the RecyclerView widget when you have lists with elements that change dynamically.

You also have the flexibility to define custom layout managers and animations for this widget.

So in the future we'll probably don't use ListViews anymore. Furthermore the RecyclerView doesn't need anything like "notifyDataSetChanged()" when an item is added or deleted from your List, which is a huge improvement performance-wise.
If you wanna know more what Google means by saying "more advanced and flexible version of ListView" I'd recommend you to read this Blog-Post A First Glance at Android’s RecyclerView.

But to answer your questions explicity:

Is there any advantages of converting the existing Listview in my app to RecyclerView?
Yes. As already said the RecyclerView is much more flexible and comes with handy animations at no cost. (In terms of doing this on your own)

When should I be using RecyclerView?
Everywhere where a ListView is appropriate a RecyclerView is as well.

And an additional question:
Is it necessary to switch from ListView to RecyclerView?
At the moment absolutely not! You have to consider that the RecyclerView isn't final at this point. So there may be some changes, bug-fixes and new features in the near future. (As an example for a bug: clipToPadding doesn't work atm. Luckily it has been reported already)




回答2:


I will try to give a detailed answer to your question.

With the advent of Android Lollipop, the RecyclerView made its way officially. The RecyclerView is much more powerful, flexible and a major enhancement over ListView. I will try to give you a detailed insight into it.

1) ViewHolder Pattern

In a ListView, it was recommended to use the ViewHolder pattern but it was never a compulsion. In case of RecyclerView, this is mandatory using the RecyclerView.ViewHolder class. This is one of the major differences between the ListView and the RecyclerView.

It makes things a bit more complex in RecyclerView but a lot of problems that we faced in the ListView are solved efficiently.

Verdict

If you are already using the ViewHolder pattern in your ListViews, then there will be no change by switching to the RecyclerView.

2) LayoutManager

This is another massive enhancement brought to the RecyclerView. In a ListView, the only type of view available is the vertical ListView. There is no official way to even implement a horizontal ListView.

Now using a RecyclerView, we can have a

i) LinearLayoutManager - which supports both vertical and horizontal lists,

ii) StaggeredLayoutManager - which supports Pinterest like staggered lists,

iii) GridLayoutManager - which supports displaying grids as seen in Gallery apps.

And the best thing is that we can do all these dynamically as we want.

Verdict

If your app demands this kind of a dynamic feature then I highly recommend you to use the RecyclerView. There are other ways to do this too, but the using the RecyclerView is the simplest way.

3) Item Animator

ListViews are lacking in support of good animations, but the RecyclerView brings a whole new dimension to it. Using the RecyclerView.ItemAnimator class, animating the views becomes so much easy and intuitive.

Verdict

We can also animate the items of a ListView and its not that difficult too, so not a strong reason to switch.

4) Item Decoration

In case of ListViews, dynamically decorating items like adding borders or dividers was never easy. But in case of RecyclerView, the RecyclerView.ItemDecorator class gives huge control to the developers but makes things a bit more time consuming and complex.

Verdict

We can do this in a ListView too and if you do not need that much customization in decorations, then the ListView can stay with you.

5) OnItemTouchListener

Intercepting item clicks on a ListView was simple, thanks to its AdapterView.OnItemClickListener interface. But the RecyclerView gives much more power and control to its developers by the RecyclerView.OnItemTouchListener but it complicates things a bit for the developer.

Verdict

This is one of the most "unnecessarily" complex things I found in RecyclerView which is way more simpler in case of ListViews.




回答3:


The RecyclerView will be the future, and it is more than a ListView2 and the @reVeres's answer explain well the case.

It is the time to check the documentation and to do a little test, but it is not the time to switch, because the RecyclerView is the support-v21 and you can't use it in a production release.



来源:https://stackoverflow.com/questions/25830472/should-i-change-the-existing-listview-in-my-app-to-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!