RecyclerView 相比 ListView 有什么增强?

What's the enhancement of RecyclerView over ListView?

RecyclerView自AndroidAPI22正式加入v7 support library。许多人说它是对 ListView 的增强,并且在互联网上发布了许多关于它的用法的简要介绍。但这些文章大多非常简单、肤浅和空洞。增强只是 RecyclerView.ViewHolderRecyclerView.ItemAnimatorRecylerView.SmoothScroller?滚动期间 recycling and reuse mechanism 项的视图是否与 ListView 不同? RecyclerView 超过 ListViewenhancement 到底是什么?

欢迎提供任何答案、提示或链接。提前致谢。

根据官方文档,RecyclerView 是对 ListView 的重大改进。它包含许多新功能,如 ViewHolderItemDecoratorLayoutManagerSmoothScroller。但是肯定使它优于 ListView 的一件事是;添加或删除项目时具有动画的能力。

视图持有者

In ListView, defining view holders was a suggested approach for keeping references for views. But it was not a compulsion. Although by not doing so, ListView used show stale data. Another major drawback of not using view holders could lead to a heavy operation of finding views by ids every time. Which resulted in laggy ListViews.

This problem is solved in RecylerView by the use of RecyclerView.ViewHolder class. This is one of the major differences in RecyclerView and ListView. When implementing a RecyclerView this class is used to define a ViewHolder object which is used by the adapter to bind ViewHolder with a position. Another point to be noted here, is that while implementing the adapter for RecyclerView, providing a ViewHolder is compulsory. This makes the implementation a little complex, but solves the issues faced in ListView.

布局管理器

When speaking of ListViews, only one type of ListView is available i.e. the vertical ListView. You cannot implement a ListView with horizontal scroll. I know there are ways to implement a horizontal scroll, but believe me it was not designed to work that way.

But now when we look at Android RecyclerView vs ListView, we have support for horizontal collections as well. In-fact it supports multiple types of lists. To support multiple types of lists it uses RecyclerView.LayoutManager class. This is something new that ListView does not have. RecyclerView supports three types of predefined Layout Managers:

LinearLayoutManager – This is the most commonly used layout manager in case of RecyclerView. Through this, we can create both horizontal and vertical scroll lists.
StaggeredGridLayoutManager – Through this layout manager, we can create staggered lists. Just like the Pinterest screen.
GridLayoutManager– This layout manager can be used to display grids, like any picture gallery.

物品动画师

Animations in a list is a whole new dimension, which has endless possibilities. In a ListView, as such there are no special provisions through which one can animate, addition or deletion of items. Instead later on as android evolved ViewPropertyAnimator was suggested by Google’s Chet Haase in this video tutorial for animations in ListView. On the other hand comparing Android RecyclerView vs ListView, it has RecyclerView.ItemAnimator class for handling animations. Through this class custom animations can be defined for item addition, deletion and move events. Also it provides a DefaultItemAnimator, in case you don’t need any customizations.

适配器

ListView adapters were simple to implement. They had a main method getView where all the magic used to happen. Where the views were bound to a position. Also they used to have an interesting method registerDataSetObserver where one can set an observer right in the adapter. This feature is also present in RecyclerView, but RecyclerView.AdapterDataObserver class is used for it. But the point in favor of ListView is that it supports three default implementations of adapters:
ArrayAdapter
CursorAdapter
SimpleCursorAdapter
Whereas RecyclerView adapter, has all the functionality that ListView adapters had except the built in support for DB cursors and ArrayLists. In RecyclerView.Adapter as of now we have to make a custom implementation to supply data to the adapter. Just like a BaseAdapter does for ListViews. Although if you wish to know more about RecyclerView adapter implementation, please refer to Android RecyclerView Example.

物品装饰

To display custom dividers in a ListView, one could have easily added these parameters in the ListView XML:
android:divider="@android:color/transparent" android:dividerHeight="5dp"
The interesting part about Android RecyclerView is that, as of now it does not show a divider between items by default. Although the guys at Google must have left this out for customization, intentionally. But this greatly increases the effort for a developer. If you wish to add a divider between items, you may need to do a custom implementation by using RecyclerView.ItemDecoration class. Or you can apply a hack by using this file from official samples: DividerItemDecoration.java

  1. View Holders

    RecylerView by the use of RecyclerView.ViewHolder class. This is one of the major differences in RecyclerView and ListView. When implementing a RecyclerView this class is used to define a ViewHolder object which is used by the adapter to bind ViewHolder with a position

  2. Layout Manager

    support for horizontal collections as well. In-fact it supports multiple types of lists. To support multiple types of lists it uses RecyclerView.LayoutManager class. This is something new that ListView does not have. RecyclerView supports three types of predefined Layout Managers: LinearLayoutManager – This is the most commonly used layout manager in case of RecyclerView. Through this, we can create both horizontal and vertical scroll lists. StaggeredGridLayoutManager – Through this layout manager, we can create staggered lists. Just like the Pinterest screen. GridLayoutManager– This layout manager can be used to display grids, like any picture gallery.

  3. Item Animator

    RecyclerView has RecyclerView.ItemAnimator class for handling animations. Through this class custom animations can be defined for item addition, deletion and move events. Also it provides a DefaultItemAnimator, in case you don’t need any customizations.

  4. Adapter

    In RecyclerView.Adapter as of now we have to make a custom implementation to supply data to the adapter. Just like a BaseAdapter does for ListViews.

来源:http://www.truiton.com/2015/03/android-recyclerview-vs-listview-comparison/

And what exactly is the enhancement of RecyclerView over ListView?

严格来说,

RecyclerView 不是 "enhancement" "over ListView"。 ListView 确实有所作为; RecyclerView,就其本身而言,不会。更准确的比较是 RecyclerView 框架 是对 AdapterView 的改进,并且在某种程度上 AbsListView parent class ListViewGridView

RecyclerView 专注于小部件回收和总体 child View 管理。它将其他一切委托给其他 classes。 AdapterView 做的少得多,因此更难扩展功能。

注意事项:

  • RecyclerView 的可滚动 space 中布置 children 委托给经理。因此,不仅可以使用 recyclerview-v7(列表、网格、交错网格)实现三个,而且还可以针对替代场景开发其他的(例如,重叠 children,StackViewGallery有点经验)。

  • 来自适配器的更新可以fine-grained。使用 AdapterView,您几乎必须在任何重要更改时重新绘制整个视图(例如,ListView 及其所有行),尤其是在添加和删除项目时。 RecyclerView 适配器中的更新机制指示更改的具体位置。这不仅需要更少的处理时间,而且有助于启用 RecyclerView 提供的用于添加、移动和删除项目的动画效果(同样,使用可插入替换)。

  • 其他 "baked into" ListView 的东西,例如绘图分隔线,现在被拉出到扩展点,例如 ItemDecorator。现在,您可以选择如何 "decorate" 项目,使用分隔线或框或彩色条分隔符或其他任何东西。装饰不限于 "dividers",但可以影响视图中的任何内容,出于某种原因,您认为与项目视图本身是分开的。

但是,

RecyclerView 开始起来相当复杂。你从 ListView "out of the box" 得到的东西需要更多的代码——你的或 third-party 库的——来匹配。对于有经验的开发人员来说,这是一个功能,因为代码可以用其他代码替换。对于新手来说,这是一个错误,因为 RecyclerView 的学习曲线更陡峭,恕我直言。