问题描述
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() - 解决方案
列表项之间没有分隔线 - 解决方案
No divider between list items - solution
没有内置重叠选择器,点击列表项时没有视觉反馈 - 解决方案
No built-in overlap selector, there is no visual feedback when you click list item - solution
列表标题没有 addHeaderView - 解决方案
也许还有更多问题...
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
.
问题:
- 我们完全用
RecyclerView
替换ListView
值得吗? - 如果不是,那么在哪种情况下我们应该更好地使用
RecyclerView
而不是ListView
,反之亦然?
推荐答案
如果 ListView 适合您,则没有理由迁移.如果您正在编写新的 UI,最好使用 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 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!