RecyclerView对ListView的增强是什么? [英] What's the enhancement of RecyclerView over ListView?
问题描述
RecyclerView
已添加到v7 support library
中.许多人说,它是对ListView
的增强,并且在Internet上发布了许多有关它的用法的简短介绍.但是这些文章大多数都非常简单,浅而空心.增强功能只是RecyclerView.ViewHolder
,RecyclerView.ItemAnimator
或RecylerView.SmoothScroller
?滚动期间项目视图的recycling and reuse mechanism
是否与ListView
的不同? 到底是RecyclerView
的enhancement
是什么?
RecyclerView
is added into v7 support library
since Android API 22 officially. And many people said that it is a enhancement over ListView
and many brief introductions to the usage of it were posted over the internet. But most of these articles are very simple, shallow and hollow. The enhancement is just RecyclerView.ViewHolder
, RecyclerView.ItemAnimator
or RecylerView.SmoothScroller
? Did the recycling and reuse mechanism
of items' views during scrolling differ from the ListView
's? And what exactly is the enhancement
of RecyclerView
over ListView
?
任何答案,技巧或链接都将受到赞赏.预先感谢.
Any answers, tips or links are appreciated. Thanks in advance.
推荐答案
根据官方文档RecyclerView
,它是对ListView
的重大改进.它包含许多新功能,例如ViewHolder
,ItemDecorator
,LayoutManager
和SmoothScroller
.但是肯定可以使它胜过ListView
的一件事是:在添加或删除项目时具有动画的能力.
As per the official documentation RecyclerView
is a major enhancement over ListView
. It contains many new features like ViewHolder
, ItemDecorator
, LayoutManager
, and SmoothScroller
. But one thing that certainly gives it an edge over the ListView
is; the ability to have animations while adding or removing an item.
查看持有人
在
ListView
中,定义视图持有人是建议的方法 保留参考意见.但这不是强迫.虽然通过 不这样做,ListView
用于显示陈旧数据.的另一个主要缺点 不使用视图持有者可能会导致查找的繁重操作 每次按ID观看次数.导致ListView
s呆滞.
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 laggyListView
s.
使用RecylerView
解决了此问题
RecyclerView.ViewHolder 类.这是在
RecyclerView
和ListView
.在实现RecyclerView
此类时
用于定义适配器使用的ViewHolder
对象
将ViewHolder
绑定到一个位置.这里要注意的另一点是
在为RecyclerView
实现适配器时,提供了一个
ViewHolder
是强制性的.这使得实现有点
复杂,但解决了ListView
中遇到的问题.
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
.
布局管理器
说到
ListView
时,只有一种类型的ListView
可用 即垂直ListView
.您不能通过以下方式实现ListView
水平滚动.我知道有一些方法可以实现横向 滚动,但请相信我,它并非旨在那样工作.
When speaking of
ListView
s, only one type ofListView
is available i.e. the verticalListView
. You cannot implement aListView
with horizontal scroll. I know there are ways to implement a horizontal scroll, but believe me it was not designed to work that way.
但是现在当我们看Android RecyclerView
vs ListView
时,我们有了
也支持水平集合.实际上支持
多种类型的列表.为了支持多种类型的列表,它使用
RecyclerView.LayoutManager
类.这是ListView的新功能
不具有. RecyclerView
支持三种类型的预定义版面
经理:
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 –这是该版本中最常用的布局管理器
RecyclerView的情况.通过这种方式,我们可以创建水平和垂直
垂直滚动列表.
StaggeredGridLayoutManager –通过此操作
布局管理器,我们可以创建交错列表.就像Pinterest
屏幕.
GridLayoutManager –此布局管理器可用于显示
网格,就像任何图片库一样.
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.
Item Animator
列表中的动画是一个全新的维度,它无穷无尽 可能性.因此,在ListView中没有特殊规定 通过它可以制作动画,添加或删除项目.反而 后来,随着android的发展, ViewPropertyAnimator 提出了 Google的Chet Haase在视频教程中,用于
ListView
中的动画. 另一方面,比较AndroidRecyclerView
与ListView
,它具有 RecyclerView.ItemAnimator 类,用于处理动画.通过这个 可以定义类自定义动画,以用于项目添加,删除和 移动事件.此外,它还提供了 DefaultItemAnimator ,以防您不 需要任何自定义.
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 AndroidRecyclerView
vsListView
, 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
适配器易于实现.他们有一个主要方法getView
曾经发生过的所有魔术.绑定视图的位置 到一个位置.他们以前也有一个有趣的方法 registerDataSetObserver ,您可以在其中设置观察者 适配器.此功能也存在于RecyclerView中,但是 为此使用了 RecyclerView.AdapterDataObserver 类.但是重点 支持ListView的是它支持三种默认实现 适配器的数量:
ArrayAdapter
CursorAdapter
SimpleCursorAdapter
而RecyclerView
适配器,具有ListView
适配器具有的所有功能,但 对DB游标和ArrayList的内置支持.在RecyclerView.Adapter
到现在为止,我们必须进行自定义实现 为适配器提供数据.就像BaseAdapter所做的一样ListView
s.虽然如果您想进一步了解RecyclerView
适配器的实现,请参考 Android RecyclerView示例.
ListView
adapters were simple to implement. They had a main methodgetView
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
WhereasRecyclerView
adapter, has all the functionality thatListView
adapters had except the built in support for DB cursors and ArrayLists. InRecyclerView.Adapter
as of now we have to make a custom implementation to supply data to the adapter. Just like a BaseAdapter does forListView
s. Although if you wish to know more aboutRecyclerView
adapter implementation, please refer to Android RecyclerView Example.
物品装饰
要在ListView中显示自定义分隔线,可以轻松添加 ListView XML中的以下参数:
android:divider="@android:color/transparent"
android:dividerHeight="5dp"
关于Android的有趣部分 RecyclerView的功能是,截至目前,它并未显示分隔符 默认情况下.尽管Google的员工一定没有这样做 进行有意定制.但这大大增加了 开发人员的努力.如果您希望在项目之间添加分隔线, 您可能需要使用进行自定义实现 RecyclerView.ItemDecoration 类.或者您可以通过使用 此文件来自官方样本:DividerItemDecoration.java
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
这篇关于RecyclerView对ListView的增强是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!