ListView 和 RecyclerView 的对比分析

2021-12-16 17:31:43 浏览数 (1)

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机制后面有机会我会专门写一篇文章来介绍,这里先简单介绍。

代码语言:javascript复制
    @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机制
代码语言:javascript复制
/**
     * 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!

0 人点赞