安卓开发相关高级UIandroid开发技巧

RecyclerView使用BRVAH完成分组布局效果

2019-11-10  本文已影响0人  难得糊涂与君勉

效果

展示在手机上效果:

示例效果图

服务器返回的数据:

示例服务器返回数据

思路

思路一:
   固定写死在xml中,然后根据相对应的去赋值,但是,这样耗时并且不够灵活,如果当前json一旦改变那么,需要在操作,果断放弃、
思路二:
ReyclerView多套布局实现。针对于这样json,需要人为干预一下服务器返回的数据源,通过for循环遍历,在每一个photoGroupID改变之前,
添加新的数据源,以此当做第二套布局等等等来完成,不是此处重点、
思路三:
借助BRVAH框架中对于Section分组布局来完成

分析

正常使用RecyclerView考虑一下几点:
(1) item条目布局样式
(2) 实体类、
(3)适配器、
(4)数据源样式、

(1)item条目布局样式

该项比较简单,根据效果图,不难看出应该准备两套布局、
第一套(分组布局头):


分组布局头

第二套(分布布局内容):


分组布局内容

比较简单,写xml就ok 了

(2)实体类

一般的RecyclerView需求中,建立一个bean:

public class PhotoItemBean{
        private int PhotoGroupID;
        private String PhotoGroupName;
        private int PhotoID;
        private String PhotoName;
        private String ImgUrl;
...set/get方法这里忽略了...
}

但是需要添加进行加一层处理,可以理解为添加一层包装:

public class ZXPTongGuoXiangXiBean extends SectionEntity<ZXPTongGuoXiangXiSecondInfo.PhotoItemsBean> {
    public ZXPTongGuoXiangXiBean(boolean isHeader, String header) {
        super(isHeader, header);
    }

    public ZXPTongGuoXiangXiBean(ZXPTongGuoXiangXiSecondInfo.PhotoItemsBean photoItemsBean) {
        super(photoItemsBean);
    }
}

自定义一个类,必须继承SectionEntity<正常adapter中用到的实体类>
可以看出提供了两个构造方法。一个是表明是header(包含header的名字),一个是正常的内容

(4)适配器样式
这里的适配器也替换成如下样式:

public class ZXPTongGuoXiangXiAdapter extends BaseSectionQuickAdapter<ZXPTongGuoXiangXiBean, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param layoutResId      The layout resource id of each item.
     * @param sectionHeadResId The section head layout id for each item
     * @param data             A new list is created out of this one to avoid mutable list
     */
    public ZXPTongGuoXiangXiAdapter(int layoutResId, int sectionHeadResId, List<ZXPTongGuoXiangXiBean> data) {
        super(layoutResId, sectionHeadResId, data);
    }

    @Override
    protected void convertHead(BaseViewHolder helper, ZXPTongGuoXiangXiBean item) {
        helper.setText(R.id.textView_ZXPG_XiangXi_TongGuo_Header, item.header);
    }

    @Override
    protected void convert(BaseViewHolder helper, ZXPTongGuoXiangXiBean item) {
        ZXPTongGuoXiangXiSecondInfo.PhotoItemsBean listbean = item.t;
        //省略其他操作,就是配置数据的操作
    }
}

可以发现,需要继承BaseSectionQuickAdapter适配器,其中第一个范型就是重新自定义的类
构造函数也需要传递:第一个是内容的layout,第二个传递的是头布局
然后也有两个处理的方法,针对于头布局以及内容区域,分别是convertHead与convert的回调。

(5)数据源
通过上述适配器也可以发现,需要List<ZXPTongGuoXiangXiBean>,但是,ZXPTongGuoXiangXiBean是自己自定义的一个类(其继承了SectionEntity),正常网络返回的就是文章开头的数据结构,我建立了PhotoItemsBean,所以这里就需要针对于json转换成的bean进行转换。一般是写一个for循环。比如我这里就是根据:

for (i in zxpTongGuoXiangXiSecondInfo?.photoItems!!.indices) {
       if (lin_code != zxpTongGuoXiangXiSecondInfo!!.photoItems.get(i).photoGroupID) {
            lin_code = zxpTongGuoXiangXiSecondInfo!!.photoItems.get(i).photoGroupID
           //往数据集合中添加
            mList.add(ZXPTongGuoXiangXiBean(true, zxpTongGuoXiangXiSecondInfo?.photoItems?.get(i)?.photoGroupName))
          }
        mList.add(ZXPTongGuoXiangXiBean(zxpTongGuoXiangXiSecondInfo?.photoItems?.get(i)))

  }

遍历数据源,根据photGourpID进行区分,如果当前的photoGroupID是新的那么就是新的一组,在当前位置插入(mList中)一个头布局,但是由于在for循环下,数据源中的数据也要继续添加,否则会少的。
具体的需要看服务器返回来的数据进行处理,简单讲就是在合适的地方插入头布局,以此达到分组的效果、

总结

该种模式针对于Section样式比较简单快捷。

上一篇下一篇

猜你喜欢

热点阅读