问题描述
Android的文档说:
Android Docs say:
的RecyclerView插件是一种更先进的和柔性版 列表显示。这个小部件是一个容器,用于显示大型数据集 可以非常有效地通过保持有限数量的滚动 的意见。使用RecyclerView小工具,当你有数据收集 其元素改变在运行时根据用户操作或网络 事件
实际上的ListView
能做到以上所有,如果效率没关系,我们已经发现了许多问题,当我们使用 RecyclerView
来代替的ListView
:
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
:
-
没有onItemClickListener()的列表项选择 - solution
列表项之间没有分隔 - solution
No divider between list items - solution
没有内置重叠选择,还有就是当你点击列表项没有视觉反馈 - 解决方案
No built-in overlap selector, there is no visual feedback when you click list item - solution
没有 addHeaderView 作为列表标题 - solution
No addHeaderView for list header - solution
也许更多的问题......
Maybe more issues ...
因此,当我们使用 RecyclerView
来代替的ListView
,我们必须做很多额外的编码,以达到同样的效果为的ListView
。
So when we use RecyclerView
to replace ListView
, we have to do much extra coding to reach the same effect as ListView
.
问:
- 是否值得我们替换
的ListView
与RecyclerView
完全? - 如果没有,那么在这种情况下,我们应该更好地利用
RecyclerView
,而不是的ListView
,反之亦然?
感谢您的关注和任何想法!
Thanks for your attention and any idea!
推荐答案
如果ListView控件为你工作,没有理由迁移。如果你正在编写一个新的用户界面,你可能会更好用RecyclerView。
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是强大的,当你需要自定义列表,或者你想更好的动画。在ListView的那些方便的方法造成了很大的麻烦,人这就是为什么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.
您需要迁移的主要变化是在您的适配器。如果你想保持呼叫 notifyDataSetChanged
,你失去了大部分的动画和放大器;绑定的好处。但是,如果你可以改变你的适配器分派具体通知事件(添加/删除/移动/更新),那么你会得到更好的动画效果和性能。这些事件让RecyclerView选择正确的动画,同时也帮助它避免不必要的 onBind
调用。你会得到一个巨大的好处,如果你的产品的看法是复杂的。此外,展望未来,将有大约RecyclerView多个组件。
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.
这篇关于如果我们用RecyclerView更换的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!