RecyclerView+BaseRecyclerViewAdapterHelper的基本使用

来源:互联网 发布:企明星软件 编辑:程序博客网 时间:2024/06/14 20:08

RecyclerView是google2014年推出 替代ListView 和GridView, RecyclerView内部维护了ViewHolder,所以在性能上比ListView 和GridView性能更高。

RecyclerView放在V7兼容包中,使用时需要依赖相应的V7包。

         LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);//      linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);//      GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);        recyclerView.setLayoutManager(staggeredGridLayoutManager);//      recyclerView.setHasFixedSize(true);        MyRecyclerAdapter adapter = new MyRecyclerAdapter(datas);        recyclerView.setAdapter(adapter);


gitHub上的开源项目BaseRecyclerViewAdapterHelper ,它可以帮助我们更好的使用RecyclerView,特点:

优化Adapter代码(减少百分之70%代码) 
添加点击item点击、长按事件、以及item子控件的点击事件 
添加加载动画(一行代码轻松切换5种默认动画) 
添加头部、尾部、下拉刷新、上拉加载(感觉又回到ListView时代) 
设置自定义的加载更多布局 
添加分组(随心定义分组头部) 
自定义不同的item类型(简单配置、无需重写额外方法) 
设置空布局(比Listview的setEmptyView还要好用!) 
添加拖拽item

如何使用它?

先在 build.gradle 的 repositories 添加:

   allprojects {        repositories {            ...            maven { url "https://jitpack.io" }        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

然后增加dependencies

dependencies {   compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.27'}

一.实现多种样式Item布局

   1.首先data的实体类implementsMultiItemEntity接口

 并实现getItemType()方法,该方法返回当前Item的类型。

 


2.自定义adapter继承 BaseMultiItemQuickAdapter类,并在构造方法中使用addItemType(type,layout)添加每个类型所对应的布局文件


3.在adapter的convert方法中通过BaseViewHolder.getItemViewType()获取当前条目的类型,进行相应的逻辑处理。



二.实现分组布局

1.新建一个实体类 extendsSectionEntity并提供两个构造方法,一个用于创建 “分组标题”一个用于创建“分组内容”


2.新建adapter 继承 BaseSectionQuickAdapter 


 head 的布局文件ID 、section的布局文件ID和数据集

在convertHeadr()方法中初始化每个head内容。


在convert中初始化每个section内容。



未完待续...



原创粉丝点击