RecyclerView 与 ListView [英] RecyclerView vs. ListView

查看:39
本文介绍了RecyclerView 与 ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 android 开发者(创建列表和卡片):><块引用>

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

好吧,听起来很酷,但是当我看到这个示例图片时,我对这两者之间的区别感到非常困惑.

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

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

解决方案

RecyclerView 是作为 ListView 改进而创建的,所以是的,您可以使用 ListView 控件,但使用 RecyclerView 更容易,因为它:

  1. 在向上/向下滚动时重用单元格 - 这可以通过在 ListView 适配器中实现 View Holder 来实现,但它是一个可选的东西,而在RecycleView 这是编写适配器的默认方式.

  2. 将列表与其容器分离 - 这样您就可以在运行时通过设置 LayoutManager 轻松地将列表项放入不同的容器(linearLayout、gridLayout)中.

示例:

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

  1. 动画常见列表动作 - 动画被解耦并委托给 ItemAnimator.

关于 RecyclerView 的内容还有很多,但我认为这几点是主要的.

所以,总而言之,RecyclerView 是一种更灵活的控件,用于处理列表数据",它遵循关注点委托模式,只为自己留下一项任务 - 回收项目.

From android developer (Creating Lists and Cards):

The RecyclerView widget is a more advanced and flexible version of ListView.

Okay, it sounds cool, but when I saw this example picture, I got really confused about the difference between these two.

The picture above can be easily created by ListView using custom adapter.

So, in what situation should one use RecyclerView?

解决方案

RecyclerView was created as a ListView improvement, so yes, you can create an attached list with ListView control, but using RecyclerView is easier as it:

  1. Reuses cells while scrolling up/down - this is possible with implementing View Holder in the ListView adapter, but it was an optional thing, while in the RecycleView it's the default way of writing adapter.

  2. Decouples list from its container - so you can put list items easily at run time in the different containers (linearLayout, gridLayout) with setting LayoutManager.

Example:

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

  1. Animates common list actions - Animations are decoupled and delegated to ItemAnimator.

There is more about RecyclerView, but I think these points are the main ones.

So, to conclude, RecyclerView is a more flexible control for handling "list data" that follows patterns of delegation of concerns and leaves for itself only one task - recycling items.

这篇关于RecyclerView 与 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆