来自android开发者(创建列表和卡片):
RecyclerView小部件是一个更高级和灵活的版本 列表视图。
好吧,这听起来很酷,但当我看到这张示例图片时,我真的很困惑这两者之间的区别。
上面的图片可以通过ListView使用自定义适配器轻松创建。
那么,在什么情况下应该使用RecyclerView呢?
来自android开发者(创建列表和卡片):
RecyclerView小部件是一个更高级和灵活的版本 列表视图。
好吧,这听起来很酷,但当我看到这张示例图片时,我真的很困惑这两者之间的区别。
上面的图片可以通过ListView使用自定义适配器轻松创建。
那么,在什么情况下应该使用RecyclerView呢?
当前回答
简单的回答:在想要显示许多项,并且它们的数量是动态的情况下,应该使用RecyclerView。ListView应该只在项目数量总是相同并且受屏幕大小限制的情况下使用。
你会发现这很难,因为你只考虑Android库。
现在有很多选项可以帮助您构建自己的适配器,使您可以轻松地构建动态项目的列表和网格,您可以选择、重新排序、使用动画、分隔符、添加页脚、页眉等等。
不要害怕,给一个尝试RecyclerView,你可以开始喜欢它从一个ListView和一个RecyclerView中从网络下载的100个项目的列表,当你尝试滚动时,你会看到UX(用户体验)的差异,可能测试应用程序会在你甚至可以这样做之前停止。
我建议你检查这两个库来制作简单的适配器:
mikepenz的FastAdapter
davideas的FlexibleAdapter
其他回答
主要优势:
ViewHolder在ListView中默认是不可用的。我们将在getView()中显式地创建。 RecyclerView有内置的Viewholder。
为了使列表视图具有良好的性能,您需要实现holder模式,这很容易搞砸,特别是当您想用几种不同类型的视图填充列表时。
RecyclerView烘焙了这个模式,使它更难以搞砸。它也更灵活,更容易处理不同的布局,不是直线,如网格。
简单的回答:在想要显示许多项,并且它们的数量是动态的情况下,应该使用RecyclerView。ListView应该只在项目数量总是相同并且受屏幕大小限制的情况下使用。
你会发现这很难,因为你只考虑Android库。
现在有很多选项可以帮助您构建自己的适配器,使您可以轻松地构建动态项目的列表和网格,您可以选择、重新排序、使用动画、分隔符、添加页脚、页眉等等。
不要害怕,给一个尝试RecyclerView,你可以开始喜欢它从一个ListView和一个RecyclerView中从网络下载的100个项目的列表,当你尝试滚动时,你会看到UX(用户体验)的差异,可能测试应用程序会在你甚至可以这样做之前停止。
我建议你检查这两个库来制作简单的适配器:
mikepenz的FastAdapter
davideas的FlexibleAdapter
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,尽管你可能需要做更多的编码工作。
除了以上差异之外,还有以下几个:
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.