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

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

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

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

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


当前回答

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的改进,所以是的,你可以创建一个附加列表与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是处理“列表数据”的一个更灵活的控件,它遵循关注点的委托模式,只留给自己一个任务——回收项目。

ListView和RecyclerView之间有很多区别,但你应该特别注意以下几点:

ViewHolder模式在ListView中是完全可选的,但它被烘焙到RecyclerView中。 ListView只支持垂直滚动,但RecyclerView并不局限于垂直滚动列表。

主要优势:

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

为了使列表视图具有良好的性能,您需要实现holder模式,这很容易搞砸,特别是当您想用几种不同类型的视图填充列表时。

RecyclerView烘焙了这个模式,使它更难以搞砸。它也更灵活,更容易处理不同的布局,不是直线,如网格。