来自android开发者(创建列表和卡片):
RecyclerView小部件是一个更高级和灵活的版本 列表视图。
好吧,这听起来很酷,但当我看到这张示例图片时,我真的很困惑这两者之间的区别。
上面的图片可以通过ListView使用自定义适配器轻松创建。
那么,在什么情况下应该使用RecyclerView呢?
来自android开发者(创建列表和卡片):
RecyclerView小部件是一个更高级和灵活的版本 列表视图。
好吧,这听起来很酷,但当我看到这张示例图片时,我真的很困惑这两者之间的区别。
上面的图片可以通过ListView使用自定义适配器轻松创建。
那么,在什么情况下应该使用RecyclerView呢?
为了使列表视图具有良好的性能,您需要实现holder模式,这很容易搞砸,特别是当您想用几种不同类型的视图填充列表时。
RecyclerView烘焙了这个模式,使它更难以搞砸。它也更灵活,更容易处理不同的布局,不是直线,如网格。
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是一个新的ViewGroup,准备渲染任何 以类似的方式创建基于适配器的视图。它应该是 ListView和GridView的继承者,它可以在 最新的support-v7版本。已经开发了RecyclerView 考虑到可扩展性,因此可以创建任何类型的 你能想到的布局,但也不能没有一点恼人的地方 剂量。
安东尼奥·莱瓦的回答
compile 'com.android.support:recyclerview-v7:27.0.0'
RecyclerView确实是一个比ListView强大的视图。 欲了解更多详情,请访问此页面。
ListView是RecyclerView的祖先。有很多事情ListView没有做,或者做得不好。如果您要收集ListView的缺点,并通过将问题抽象到不同的域来解决问题,那么您最终会得到类似于回收器视图的东西。下面是ListViews的主要问题点:
Didn't enforce View Reuse for same item types (look at one of the adapters that are used in a ListView, if you study the getView method you will see that nothing prevents a programmer from creating a new view for every row even if one is passed in via the convertView variable) Didn't prevent costly findViewById uses(Even if you were recycling views as noted above it was possible for devs to be calling findViewById to update the displayed contents of child views. The main purpose of the ViewHolder pattern in ListViews was to cache the findViewById calls. However this was only available if you knew about it as it wasn't part of the platform at all) Only supported Vertical Scrolling with Row displayed Views (Recycler view doesn't care about where views are placed and how they are moved, it's abstracted into a LayoutManager. A Recycler can therefore support the traditional ListView as shown above, as well as things like the GridView, but it isn't limited to that, it can do more, but you have to do the programming foot work to make it happen). Animations to added/removed was not a use case that was considered. It was completely up to you to figure out how go about this (compare the RecyclerView. Adapter classes notify* method offerings v. ListViews to get an idea).
简而言之,RecyclerView是一个更灵活的ListView,尽管你可能需要做更多的编码工作。
主要优势:
ViewHolder在ListView中默认是不可用的。我们将在getView()中显式地创建。 RecyclerView有内置的Viewholder。
In my opinion RecyclerView was made to address the problem with the recycle pattern used in listviews because it was making developer's life more difficult. All the other you could handle more or less. For instance I use the same adapter for ListView and GridView it doesn't matter in both views the getView, getItemCount, getTypeCount is used so it's the same. RecyclerView isn't needed if ListView with ListAdapter or GridView with grid adapters is already working for you. If you have implemented correctly the ViewHolder pattern in your listviews then you won't see any big improvement over RecycleView.
I think the main and biggest difference they have is that ListView looks for the position of the item while creating or putting it, on the other hand RecyclerView looks for the type of the item. if there is another item created with the same type RecyclerView does not create it again. It asks first adapter and then asks to recycledpool, if recycled pool says "yeah I've created a type similar to it", then RecyclerView doesn't try to create same type. ListView doesn't have a this kind of pooling mechanism.
以下是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.
我用RecyclerView工作了一点,仍然喜欢ListView。
当然,它们都使用ViewHolders,所以这不是一个优势。 RecyclerView在编码方面更加困难。 RecyclerView不包含页眉和页脚,所以它是一个减号。 ListView不需要创建ViewHolder。如果你想要一个带有分段或子标题的列表,那么创建独立的项(没有ViewHolder)是个好主意,这样更容易,也不需要单独的类。
除了以上差异之外,还有以下几个:
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.
您可以使用接口来提供单击侦听器。我用这个 技术与ListViews。 无需分隔:只需在行中添加宽度为的视图 Match_parent和1dp的高度,并给它一个背景色。 简单地使用StateList选择器作为行背景。 addHeaderView也可以在ListViews中避免使用:只需将 视图外的标头。
所以,如果效率是你所关心的,那么是的,用RecyclerView替换ListView是个好主意。
简单的回答:在想要显示许多项,并且它们的数量是动态的情况下,应该使用RecyclerView。ListView应该只在项目数量总是相同并且受屏幕大小限制的情况下使用。
你会发现这很难,因为你只考虑Android库。
现在有很多选项可以帮助您构建自己的适配器,使您可以轻松地构建动态项目的列表和网格,您可以选择、重新排序、使用动画、分隔符、添加页脚、页眉等等。
不要害怕,给一个尝试RecyclerView,你可以开始喜欢它从一个ListView和一个RecyclerView中从网络下载的100个项目的列表,当你尝试滚动时,你会看到UX(用户体验)的差异,可能测试应用程序会在你甚至可以这样做之前停止。
我建议你检查这两个库来制作简单的适配器:
mikepenz的FastAdapter
davideas的FlexibleAdapter
RecyclerView相对于listview的优点:
默认包含ViewHolder。 简单的动画。 支持水平,网格和交错布局
listView相对于recyclerView的优点:
易于添加分隔线。 可以使用内置的arrayAdapter简单的普通列表 支持页眉和页脚。 支持OnItemClickListner。
RecyclerView信息
RecyclerView是在Android 5.0 (Lollipop)中引入的。它包含在支持库中。因此,它与Android API Level 7兼容。
与ListView类似,RecyclerView的主要思想是以一种性能友好的方式提供列表功能。这个视图名称中的“Recycler”部分并不是巧合。RecyclerView实际上可以回收它当前正在使用的项目。循环过程是通过一个名为View Holder的模式完成的。
RecyclerView的优点和缺点
优点:
集成的动画添加,更新和删除项目 通过使用ViewHolder模式强制循环视图 同时支持网格和列表 支持垂直和水平滚动 可以和DiffUtil一起使用吗
缺点:
增加了复杂性 没有OnItemClickListener
列表查看信息
ListView在Android诞生之初就已经存在了。它甚至可以在API级别1中使用,它与RecyclerView具有相同的用途。
ListView的用法其实很简单。在这方面,它不像它的后继者。学习曲线比RecyclerView更平滑。因此,它更容易掌握。我们不需要处理LayoutManager, ItemAnimator或DiffUtil之类的东西。
ListView的优点和缺点
优点:
简单的使用 默认的适配器 可用OnItemClickListener 它是expandabelistview的基础
缺点:
没有使用ViewHolder模式
ListView和RecyclerView之间有很多区别,但你应该特别注意以下几点:
ViewHolder模式在ListView中是完全可选的,但它被烘焙到RecyclerView中。 ListView只支持垂直滚动,但RecyclerView并不局限于垂直滚动列表。
我只是想强调RecyclerView是兼容包的一部分。这意味着每个应用程序都有自己的RecyclerView实现,而不是使用来自操作系统的特性和代码。潜在地,类似RecyclerView的功能可以成为未来操作系统的一部分,从那里使用它是有益的。例如,Harmony OS很快就会发布。将来可以更改兼容性包许可证,这可能是一种暗示。缺点总结:
许可 更大的足迹,尤其是作为许多应用程序的一部分 如果一些来自操作系统的功能可以存在,那么效率就会下降
但值得注意的是,一些功能的实现,如滑动项目,来自RecyclerView。
以上所说的一切都必须加以考虑。