来自android开发者(创建列表和卡片):

RecyclerView小部件是一个更高级和灵活的版本 列表视图。

好吧,这听起来很酷,但当我看到这张示例图片时,我真的很困惑这两者之间的区别。

上面的图片可以通过ListView使用自定义适配器轻松创建。

那么,在什么情况下应该使用RecyclerView呢?


当前回答

除了以上差异之外,还有以下几个:

RV separates view creation and binding of data to view. In LV, you need to check if convertView is null or not for creating view, before binding data to it. So, in case of RV, view will be created only when it is needed but in case of LV, one can miss the check for convertview and will create view everytime. Switching between Grid and List is more easy now with LayoutManager. No need to notify and update all items, even if only single item is changed. One had to implement view caching in case of LV. It is provided in RV by default. (There is difference between view caching n recycling.) Very easy item animations in case of RV.

其他回答

以下是RecyclerView和ListView之间的几个关键点/区别。明智地接电话。

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. RecylerView has inbuilt ViewHolder, doesn't need to implement our own like in listView. It support notify at particular index as well Things like animating the addition or removal of items are already implemented in the RecyclerView without you having to do anything We can associate a layout manager with a RecyclerView, this can be used for getting random views in recycleview while this was limitation in ListView In a ListView, the only type of view available is the vertical ListView. There is no official way to even implement a horizontal ListView. Now using a RecyclerView, we can have a i) LinearLayoutManager - which supports both vertical and horizontal lists, ii) StaggeredLayoutManager - which supports Pinterest like staggered lists, iii) GridLayoutManager - which supports displaying grids as seen in Gallery apps. And the best thing is that we can do all these dynamically as we want.

主要优势:

ViewHolder在ListView中默认是不可用的。我们将在getView()中显式地创建。 RecyclerView有内置的Viewholder。

RecyclerView被创建为一个ListView的改进,所以是的,你可以创建一个附加列表与ListView控件,但使用RecyclerView更容易,因为它:

在向上/向下滚动时重用单元格——这在ListView适配器中实现View Holder是可能的,但这是一个可选的事情,而在RecycleView中,这是默认的写适配器的方式。 解耦列表从它的容器-所以你可以很容易地把列表项在运行时在不同的容器(线性布局,gridLayout)通过设置LayoutManager。

例子:

mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//or
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));

动画常用列表动作——动画被解耦并委托给ItemAnimator。

还有更多关于RecyclerView的内容,但我认为这些要点是主要的。

因此,综上所述,RecyclerView是处理“列表数据”的一个更灵活的控件,它遵循关注点的委托模式,只留给自己一个任务——回收项目。

我只是想强调RecyclerView是兼容包的一部分。这意味着每个应用程序都有自己的RecyclerView实现,而不是使用来自操作系统的特性和代码。潜在地,类似RecyclerView的功能可以成为未来操作系统的一部分,从那里使用它是有益的。例如,Harmony OS很快就会发布。将来可以更改兼容性包许可证,这可能是一种暗示。缺点总结:

许可 更大的足迹,尤其是作为许多应用程序的一部分 如果一些来自操作系统的功能可以存在,那么效率就会下降

但值得注意的是,一些功能的实现,如滑动项目,来自RecyclerView。

以上所说的一切都必须加以考虑。

除了以上差异之外,还有以下几个:

RV separates view creation and binding of data to view. In LV, you need to check if convertView is null or not for creating view, before binding data to it. So, in case of RV, view will be created only when it is needed but in case of LV, one can miss the check for convertview and will create view everytime. Switching between Grid and List is more easy now with LayoutManager. No need to notify and update all items, even if only single item is changed. One had to implement view caching in case of LV. It is provided in RV by default. (There is difference between view caching n recycling.) Very easy item animations in case of RV.