我们应该使用 RecyclerView 来代替 ListView 吗? [英] Should we use RecyclerView to replace ListView?

查看:18
本文介绍了我们应该使用 RecyclerView 来代替 ListView 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 文档说:

Android Docs say:

RecyclerView 小部件是更高级和更灵活的版本列表显示.这个小部件是一个用于显示大数据集的容器通过保持有限的数量可以非常有效地滚动的意见.当您有数据收集时使用 RecyclerView 小部件其元素在运行时根据用户操作或网络发生变化活动

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可以做到上面所有的效果,如果效率不重要,我们在用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:

  1. 列表项选择没有 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,反之亦然?
  • Is it worth that we replace ListView with RecyclerView totally ?
  • if not then in which case should we better use RecyclerView instead ListView, and vice versa ?

推荐答案

如果 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 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆