Android Docs say:
Android医生说:
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
可回收的视图部件是一个更加高级和灵活的ListView版本。这个小部件是一个用于显示大数据集的容器,通过保持有限的视图数量,可以非常有效地滚动这些数据集。当数据集合的元素在运行时基于用户操作或网络事件发生更改时,请使用recycling视图小部件
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
:
实际上,如果效率不重要的话,ListView可以做以上所有的事情,并且我们发现当我们使用recycling视图来取代ListView时,我们发现了很多问题:
-
There is no onItemClickListener() for list item selection - solution
列表项选择解决方案没有onItemClickListener()
-
No divider between list items - solution
列表项之间没有分隔——解决方案
-
No built-in overlap selector, there is no visual feedback when you click list item - solution
没有内置的重叠选择器,当你点击列表项-解决方案时没有视觉反馈
-
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
.
所以当我们使用回收视图来替换ListView时,我们需要做很多额外的编码来达到与ListView相同的效果。
QUESTION:
问题:
- Is it worth that we replace
ListView
withRecyclerView
totally ?- 完全用回收视图替换ListView值吗?
- if not then in which case should we better use
RecyclerView
insteadListView
, and vice versa ?- 如果不是这样,我们应该使用回收视图而不是ListView,反之亦然吗?
5 个解决方案
#1
114
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.
如果ListView对您有效,那么没有理由迁移。如果您正在编写一个新的UI,您可能会更好地使用回收视图。
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.
当你需要定制你的列表或者你想要更好的动画时,回收视图是很强大的。ListView中的那些方便方法给人们带来了很多麻烦,这就是为什么recycling视图为人们提供了更灵活的解决方案。
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.
迁移所需的主要更改位于适配器中。如果您想继续调用notifyDataSetChanged,您将失去大部分动画和绑定好处。但是如果您可以更改适配器来分派详细的通知事件(添加/删除/移动/更新),那么您将获得更好的动画和性能。这些事件让回收的视图选择正确的动画,它也帮助它避免不必要的onBind调用。如果您的项目视图是复杂的,那么您将获得巨大的好处。此外,在回收视图周围还会有更多的组件。
#2
21
According to me, if ListView fulfills all the current needs of your app and satisfies all use-cases then there is no need to replace it with a RecyclerView.
在我看来,如果ListView能够满足你的应用程序的所有当前需求并满足所有用例,那么就不需要用回收视图来替换它。
The RecyclerView gives huge power to its developers at the cost of increasing the complexity for the developers. There are certain things which could be done easily in a ListView can now take a lot of unnecessary effort.
回收视图为开发人员提供了巨大的力量,代价是增加了开发人员的复杂性。有些事情可以在ListView中轻松完成,但现在需要花费大量不必要的努力。
But yes, there are many things that a ListView can never do, like the amazing LayoutManager feature which can allow you to dynamically change the layout to horizontal, vertical, grid, or staggered grid seamlessly.
但是,确实有很多事情是ListView做不到的,比如惊人的LayoutManager特性,它可以让你动态地将布局改变为水平、垂直、网格或交错网格。
I wrote a detailed answer on this topic here.
我在这里就这个话题写了一个详细的回答。
#3
8
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.
你可以使用一个接口来提供一个点击监听器。我也对listview使用这种技术。2无分隔符:只需将match_parent宽度和高度为1dp的视图添加到您的行中,并为其设置背景颜色。只需为行背景使用一个StateList选择器。addHeaderView在ListViews中也可以避免:只需将Header放在View之外。
So, if efficiency is your concern, then yes, it's a good idea to replace a ListView with a RecyclerView.
所以,如果你关心的是效率,那么用回收视图代替ListView是个好主意。
#4
3
The only case when it's still fine to use ListView is when the list is not dynamic or affected by network events. For example: navigation.
只有在列表不动态或不受网络事件影响时才可以使用ListView。例如:导航。
For any other uses, RecyclerView eclipses ListView. Since RecyclerView only cares about 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 (the stock has StaggeredGrid in addition to the old list or grid style but there's also this library which extends it even more).
对于其他用途,可回收利用eclipses ListView。自RecyclerView只关心回收会更容易做视觉紧密耦合在列表视图相关的东西,就像改变位置/重排,动画(事实上它有RecyclerView.ItemAnimator),自定义布局(股票进行除了老列表或网格风格但也有这个库延伸甚至更多)。
Also if you want to use CardView I believe it's the only way to go (some good reading when to use card or list).
另外,如果你想使用CardView,我相信这是你唯一的选择(当你使用卡片或列表时,有一些很好的阅读材料)。
#5
1
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.
一个很好的选择是使用BaseAdapter。它支持使用Viewholder模式,而我的模式包含100多行带有位图和按钮,运行非常流畅。
#1
114
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.
如果ListView对您有效,那么没有理由迁移。如果您正在编写一个新的UI,您可能会更好地使用回收视图。
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.
当你需要定制你的列表或者你想要更好的动画时,回收视图是很强大的。ListView中的那些方便方法给人们带来了很多麻烦,这就是为什么recycling视图为人们提供了更灵活的解决方案。
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.
迁移所需的主要更改位于适配器中。如果您想继续调用notifyDataSetChanged,您将失去大部分动画和绑定好处。但是如果您可以更改适配器来分派详细的通知事件(添加/删除/移动/更新),那么您将获得更好的动画和性能。这些事件让回收的视图选择正确的动画,它也帮助它避免不必要的onBind调用。如果您的项目视图是复杂的,那么您将获得巨大的好处。此外,在回收视图周围还会有更多的组件。
#2
21
According to me, if ListView fulfills all the current needs of your app and satisfies all use-cases then there is no need to replace it with a RecyclerView.
在我看来,如果ListView能够满足你的应用程序的所有当前需求并满足所有用例,那么就不需要用回收视图来替换它。
The RecyclerView gives huge power to its developers at the cost of increasing the complexity for the developers. There are certain things which could be done easily in a ListView can now take a lot of unnecessary effort.
回收视图为开发人员提供了巨大的力量,代价是增加了开发人员的复杂性。有些事情可以在ListView中轻松完成,但现在需要花费大量不必要的努力。
But yes, there are many things that a ListView can never do, like the amazing LayoutManager feature which can allow you to dynamically change the layout to horizontal, vertical, grid, or staggered grid seamlessly.
但是,确实有很多事情是ListView做不到的,比如惊人的LayoutManager特性,它可以让你动态地将布局改变为水平、垂直、网格或交错网格。
I wrote a detailed answer on this topic here.
我在这里就这个话题写了一个详细的回答。
#3
8
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.
你可以使用一个接口来提供一个点击监听器。我也对listview使用这种技术。2无分隔符:只需将match_parent宽度和高度为1dp的视图添加到您的行中,并为其设置背景颜色。只需为行背景使用一个StateList选择器。addHeaderView在ListViews中也可以避免:只需将Header放在View之外。
So, if efficiency is your concern, then yes, it's a good idea to replace a ListView with a RecyclerView.
所以,如果你关心的是效率,那么用回收视图代替ListView是个好主意。
#4
3
The only case when it's still fine to use ListView is when the list is not dynamic or affected by network events. For example: navigation.
只有在列表不动态或不受网络事件影响时才可以使用ListView。例如:导航。
For any other uses, RecyclerView eclipses ListView. Since RecyclerView only cares about 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 (the stock has StaggeredGrid in addition to the old list or grid style but there's also this library which extends it even more).
对于其他用途,可回收利用eclipses ListView。自RecyclerView只关心回收会更容易做视觉紧密耦合在列表视图相关的东西,就像改变位置/重排,动画(事实上它有RecyclerView.ItemAnimator),自定义布局(股票进行除了老列表或网格风格但也有这个库延伸甚至更多)。
Also if you want to use CardView I believe it's the only way to go (some good reading when to use card or list).
另外,如果你想使用CardView,我相信这是你唯一的选择(当你使用卡片或列表时,有一些很好的阅读材料)。
#5
1
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.
一个很好的选择是使用BaseAdapter。它支持使用Viewholder模式,而我的模式包含100多行带有位图和按钮,运行非常流畅。