ListView 和 RecyclerView 的对比分析

Android app中通过列表展示数据是非常常见的场景。 例如, IM类会话列表/消息列表就会使用列表进行数据展示。
列表数据显示如下图 :


网络图片

早期Android开发都会使用ListView来展示数据,然而现在用的已经比较少了,更多的时候都是使用RecyclerView替代ListView进行表格数据的展示,既然Google官方推出了RecyclerView,并能够被更多人用来取代ListView,那肯定是存在一定的合理性的。本篇文章就针对ListViewRecyclerView进行简单的对比和分析。

ListView

ListView是一种常用的系统控件,主要用于展示列表数据,具体的使用这里不进行过多的介绍。
这里主要介绍ListView控件 和 RecyclerView控件的一些差异,主要差异如下:
1,ListView 布局只支持纵向列表
2, ListView 需自己实现ViewHolder机制
3,ListView实现了两级缓存,提升view性能

布局

ListView 系统api只提供了纵向列表显示,无法显示横行或者网格布局等

ViewHolder机制

ListView 没有默认实现ViewHolder机制.关于ViewHolder机制后面有机会我会专门写一篇文章来介绍,这里先简单介绍。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        InputAppInfo appInfo = appList.get(position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.app_grid_item, null);
            viewHolder.image = (ImageView) convertView
                    .findViewById(R.id.app_grid_item_image);
            convertView.setTag(viewHolder);
        }
        viewHolder = (ViewHolder) convertView.getTag();
        
        /*
         *  业务逻辑代码省略
         */
        return convertView;
    }

上面的这段代码是常见的开发者在ListView中自己实现ViewHolder的典型代码.
这里ViewHolder主要解决的问题是减少View.findViewById()的调用,从而提升ListView的性能

缓存机制

ListView缓存View对象,设置有两级缓存(ActiveViewsScrapViews),缓存逻辑ListView写在RecycleBin中。

ListView缓存示意图

ActiveViews:屏幕中可见View的缓存
ScrapViews:顾名思义是已废弃的View的缓存,也就是item在滑出界面后View会被存放到ScrapViews中。

RecyclerView

相比ListViewRecyclerView在设计上考虑了更方便的扩张,同时性能上也进行了更多的优化
1,RecyclerView支持3种布局(LinearLayoutManager,GridLayoutManager,StaggeredLayouManager),具体的布局使用可以自行google查看Demo
2,RecyclerView强制实现了ViewHolder机制
3,RecyclerView实现了四级缓存,提升view性能

布局

ListView 相比,RecyclerView支持3种类型的布局,使得能够展示的数据样式更加丰富

ViewHolder机制
/**
     * Base class for an Adapter
     *
     * <p>Adapters provide a binding from an app-specific data set to views that are displayed
     * within a {@link RecyclerView}.</p>
     *
     * @param <VH> A class that extends ViewHolder that will be used by the adapter.
     */
    public abstract static class Adapter<VH extends ViewHolder> {
        private final AdapterDataObservable mObservable = new AdapterDataObservable();
        private boolean mHasStableIds = false;

        /**
         * Called when RecyclerView needs a new {@link ViewHolder} of the given type to represent
         * an item.
         * <p>
         * This new ViewHolder should be constructed with a new View that can represent the items
         * of the given type. You can either create a new View manually or inflate it from an XML
         * layout file.
         * <p>
         * The new ViewHolder will be used to display items of the adapter using
         * {@link #onBindViewHolder(ViewHolder, int, List)}. Since it will be re-used to display
         * different items in the data set, it is a good idea to cache references to sub views of
         * the View to avoid unnecessary {@link View#findViewById(int)} calls.
         *
         * @param parent The ViewGroup into which the new View will be added after it is bound to
         *               an adapter position.
         * @param viewType The view type of the new View.
         *
         * @return A new ViewHolder that holds a View of the given view type.
         * @see #getItemViewType(int)
         * @see #onBindViewHolder(ViewHolder, int)
         */
        @NonNull
        public abstract VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType);

        /**
         * Called by RecyclerView to display the data at the specified position. This method should
         * update the contents of the {@link ViewHolder#itemView} to reflect the item at the given
         * position.
         * <p>
         * Note that unlike {@link android.widget.ListView}, RecyclerView will not call this method
         * again if the position of the item changes in the data set unless the item itself is
         * invalidated or the new position cannot be determined. For this reason, you should only
         * use the <code>position</code> parameter while acquiring the related data item inside
         * this method and should not keep a copy of it. If you need the position of an item later
         * on (e.g. in a click listener), use {@link ViewHolder#getAdapterPosition()} which will
         * have the updated adapter position.
         *
         * Override {@link #onBindViewHolder(ViewHolder, int, List)} instead if Adapter can
         * handle efficient partial bind.
         *
         * @param holder The ViewHolder which should be updated to represent the contents of the
         *        item at the given position in the data set.
         * @param position The position of the item within the adapter's data set.
         */
        public abstract void onBindViewHolder(@NonNull VH holder, int position);


        ..........

}

可以看到RecyclerViewAdapter 需要传入一个 ViewHolder,ViewHolder的相关机制RecyclerView已经默认给实现了。 开发者只需要重写onCreateViewHolderonBindViewHolder 就能够很便捷的使用ViewHolder 机制。

缓存机制

RecyclerView缓存ViewHolder对象,在Recyle种实现四级缓存(ActiveViewsScrapViews

RecyclerView缓存示意图

Scrap:AttachedScrap是一级缓存。 缓存的是屏幕中可见的ViewHodler数据,(通过postion来查找)
CachedCiews:第二级缓存,将刚刚移出屏幕的item放入这里(默认2个),通过postion来查找。 找到的ViewHolder可以直接使用,不需要进行BindViewHolder()过程
ViewCacheExtension: 第三层缓存, 是用户自定义的缓存。
RecycledViewPool: 第四层缓存,是个缓存池。 多个RecyclerView对象可以共用一个RecycledViewPool, 需要经过BindViewHolder()过程,因为里面的数据都已经是脏数据了

END!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355

推荐阅读更多精彩内容