1. 程式人生 > >RecyclerView分組之BaseRecyclerViewAdapterHelper(實現分組功能)

RecyclerView分組之BaseRecyclerViewAdapterHelper(實現分組功能)

1.前言

 相信很多小夥伴之前用過listview列表展示資料,然後RecyclerView應該不會陌生,大多數開發者應該都使用上它了,它也是google推薦替換ListView的控制元件,但是用過它的同學應該都知道它在某些方面並沒有ListView使用起來方便。下面要給大家介紹的是一個開源庫BaseRecyclerViewAdapterHelper,下面我是以一個分組列表作為一個列子:

效果圖

這個列表分組可能是多個

2.如何實現它

 allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io"
} } }
 compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34'

3.下面看下josn資料

{
    "data": [
        {
            "bannerInfo": [
                {
                    "createTime": 1513221862000,
                    "id": 1,
                    "imgUrl": "/upload/banner/20171207/juxing.png"
, "isValid": 1, "lastModifiedTime": 1513221858000, "readCount": "", "seqNum": 1, "title": "APP首頁的banner", "url": "", "version": "1", "workId": "201712140201"
} ]
, "bannerType": "1", "title": "首頁banner" }, { "bannerInfo": [ { "createTime": 1513222456000, "id": 3, "imgUrl": "/upload/banner/20171207/xl.png", "isValid": 1, "lastModifiedTime": 1513222445000, "seqNum": 1, "title": "APP首頁的底部banner", "url": "http://h.sinaif.com/loginCommon?codeKey=1002_s68160033_CP001 ", "version": "1", "workId": "201712140203" } ], "bannerType": "2", "title": "廣告" }, { "bannerInfo": [ { "createTime": 1513222453000, "id": 2, "imgUrl": "/upload/banner/20171207/jx.png", "isValid": 1, "lastModifiedTime": 1513222316000, "publishDate": "2017-12-15", "readCount": "12", "seqNum": 1, "title": "APP首頁的精彩推薦", "url": "https://zhuanlan.zhihu.com/p/20881058 ", "version": "1", "workId": "201712140202" }, { "createTime": 1513240674000, "id": 4, "imgUrl": "/upload/banner/20171207/jx.png", "isValid": 1, "lastModifiedTime": 1513240677000, "publishDate": "2017-12-15", "readCount": "111", "seqNum": 2, "title": "APP首頁的精彩推薦", "url": "https://zhuanlan.zhihu.com/p/20881058 ", "version": "1", "workId": "201712140204" } ], "bannerType": "3", "title": "精彩推薦" }, { "bannerInfo": [ { "createTime": 1513307486000, "id": 5, "imgUrl": "/upload/banner/20171207/ic_block.png", "isValid": 1, "lastModifiedTime": 1513307488000, "publishDate": "2017-12-15", "readCount": "120", "seqNum": 1, "title": "APP首頁的專題推薦", "url": "https://zhuanlan.zhihu.com/p/20881058 ", "version": "1", "workId": "201712140205" }, { "createTime": 1513307589000, "id": 6, "imgUrl": "/upload/banner/20171207/jx.png", "isValid": 1, "lastModifiedTime": 1513307591000, "publishDate": "2017-12-15", "readCount": "303", "seqNum": 2, "title": "APP首頁的專題推薦", "url": "https://zhuanlan.zhihu.com/p/20881058 ", "version": "1", "workId": "201712140206" } ], "bannerType": "4", "title": "專題推薦" } ]
}

4.定義bean

package com.kawang.qx.ui.home.model;

import com.chad.library.adapter.base.entity.SectionEntity;


import java.io.Serializable;
import java.util.List;

/**
 * Created by xiaoyi 
 */

public class HomeListBean extends SectionEntity<BannerInfoBean> {


    public HomeListBean(boolean isHeader, String header) {
        super(isHeader, header);
    }

    public HomeListBean(BannerInfoBean bannerInfoBean) {
        super(bannerInfoBean);
    }
    private String bannerType;
    private String title;
    private List<BannerInfoBean> bannerInfo;

    public String getBannerType() {
        return bannerType;
    }

    public void setBannerType(String bannerType) {
        this.bannerType = bannerType;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<BannerInfoBean> getBannerInfo() {
        return bannerInfo;
    }

    public void setBannerInfo(List<BannerInfoBean> bannerInfo) {
        this.bannerInfo = bannerInfo;
    }
}
package com.kawang.qx.ui.home.model;

import java.io.Serializable;

/**
 * Created by Administrator xiaoyi
 */

public class BannerInfoBean {

    /**
     * bannerTitle : 首頁banner
     * bannerType : 1
     * createTime : 1513221862000
     * id : 1
     * imgUrl : /upload/banner/20171207/juxing.png
     * isValid : 1
     * lastModifiedTime : 1513221858000
     * seqNum : 1
     * url :
     * version : 1
     * workId : 201712140201
     */

    private String detailTitle;
    private long createTime;
    private int id;
    private String imgUrl;
    private int isValid;
    private long lastModifiedTime;
    private int seqNum;
    private String url;
    private String version;
    private String workId;
    private String title;
    private String publishDateShow;
    private String readCount;

    public String getDetailTitle() {
        return detailTitle;
    }

    public void setDetailTitle(String detailTitle) {
        this.detailTitle = detailTitle;
    }

    public String getPublishDateShow() {
        return publishDateShow;
    }

    public void setPublishDateShow(String publishDateShow) {
        this.publishDateShow = publishDateShow;
    }

    public String getReadCount() {
        return readCount;
    }

    public void setReadCount(String readCount) {
        this.readCount = readCount;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public int getIsValid() {
        return isValid;
    }

    public void setIsValid(int isValid) {
        this.isValid = isValid;
    }

    public long getLastModifiedTime() {
        return lastModifiedTime;
    }

    public void setLastModifiedTime(long lastModifiedTime) {
        this.lastModifiedTime = lastModifiedTime;
    }

    public int getSeqNum() {
        return seqNum;
    }

    public void setSeqNum(int seqNum) {
        this.seqNum = seqNum;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getWorkId() {
        return workId;
    }

    public void setWorkId(String workId) {
        this.workId = workId;
    }

    @Override
    public String toString() {
        return "BannerInfoBean{" +
                "bannerTitle='" + detailTitle + '\'' +
                ", createTime=" + createTime +
                ", id=" + id +
                ", imgUrl='" + imgUrl + '\'' +
                ", isValid=" + isValid +
                ", lastModifiedTime=" + lastModifiedTime +
                ", seqNum=" + seqNum +
                ", url='" + url + '\'' +
                ", version='" + version + '\'' +
                ", workId='" + workId + '\'' +
                ", title='" + title + '\'' +
                ", publishDate='" + publishDateShow + '\'' +
                ", readCount='" + readCount + '\'' +
                '}';
    }

    public BannerInfoBean(String imgUrl,String title, String publishDateShow, String        readCount, String url,String detailTitle) {
        this.imgUrl = imgUrl;
        this.title = title;
        this.publishDateShow = publishDateShow;
        this.readCount = readCount;
        this.url = url;
        this.detailTitle = detailTitle;
    }
}

5.下面是介面卡使用

package com.kawang.qx.adapter;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kawang.qx.R;
import com.kawang.qx.common.Constants;
import com.kawang.qx.ui.home.model.BannerInfoBean;
import com.kawang.qx.ui.home.model.HomeListBean;
import com.kawang.qx.utils.ToastUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/12/18.
 */

public class SectionAdapter extends BaseSectionQuickAdapter<HomeListBean, BaseViewHolder> {
    private Context context;

    public SectionAdapter(int layoutResId, int sectionHeadResId, List<HomeListBean> data,Context context) {
        super(layoutResId, sectionHeadResId, data);
        this.context=context;
    }

    @Override
    protected void convertHead(BaseViewHolder helper, final HomeListBean item) {
        helper.setText(R.id.tv_marvellous, item.header);
    }


    @Override
    protected void convert(BaseViewHolder helper, HomeListBean item) {
        BannerInfoBean homeListBean = item.t;
        ImageView icon = helper.getView(R.id.img_blockChain);
        Glide.with(context).load(Constants.IMAGE_URL + homeListBean.getImgUrl()).into(icon);
        helper.setText(R.id.tv_blockTitle, homeListBean.getTitle());
        helper.setText(R.id.tv_blockTime, homeListBean.getPublishDateShow());
        helper.setText(R.id.tv_blockNum, homeListBean.getReadCount());
    }
}

這裡我佈局就是不貼出來了 就是一個線性佈局…..

6.我們看下HomeFragment.Java呼叫:

 @Override
    public void showData(Object o) {
        if (o instanceof List) {
            //   mImagerList.clear();
            mBannerInfoBeen = (ArrayList<HomeListBean>) o;
            if (mBannerInfoBeen.size() > 0) {
                for (int i = 0; i < mBannerInfoBeen.size(); i++) {
                    type = mBannerInfoBeen.get(i).getBannerType();
                    if (type.equals("1")) {
                        mImagerList.addAll(mBannerInfoBeen.get(i).getBannerInfo());
                        initBanner();//首頁頭部banner
                    } else if (type.equals("2")) {
                        mBottomList.addAll(mBannerInfoBeen.get(i).getBannerInfo());
                        bottom_Title = mBannerInfoBeen.get(i).getTitle();
                    } else if (type.equals("3") || type.equals("4")) {
                        mList.add(new HomeListBean(true, mBannerInfoBeen.get(i).getTitle()));
                        for (BannerInfoBean bean : mBannerInfoBeen.get(i).getBannerInfo()) {
                            mList.add(new HomeListBean(new BannerInfoBean(bean.getImgUrl(), bean.getTitle(), bean.getPublishDateShow(), bean.getReadCount(), bean.getUrl(),bean.getDetailTitle())));
                        }
                    }
                }
                initRedata();//介面卡
            }
        }
    }

    private void initRedata() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new SectionAdapter(R.layout.item_marvellous_info, R.layout.item_home_text, mList, getActivity());
        //新增頭部
        View headerView =  getActivity().getLayoutInflater().inflate(R.layout.item_bottom_banner, null);
        headerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        TextView title = (TextView) headerView.findViewById(R.id.tv_title);
        title.setText(bottom_Title);
        for (int i = 0; i < mBottomList.size(); i++) {
            bottom_url = mBottomList.get(i).getUrl();
            banner_title = mBottomList.get(i).getDetailTitle();
            Glide.with(getActivity()).load(Constants.IMAGE_URL + mBottomList.get(i).getImgUrl()).into((ImageView) headerView.findViewById(R.id.img_banner));
        }
        if (mBottomList.size() > 0) {
            adapter.addHeaderView(headerView);
        }
        mRecyclerView.setAdapter(adapter);
        headerView.findViewById(R.id.lin_bottomBanner).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.skip(getActivity(), bottom_url, banner_title);
            }
        });
        //介面卡點選事件
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                HomeListBean homeListBean = mList.get(position);
                WebViewActivity.skip(getActivity(), homeListBean.t.getUrl(), homeListBean.t.getDetailTitle());
            }
        });
    }

這裡我講解一下上面的實現程式碼:
1.看下json資料 bannerType型別 因為後臺返回的資料型別不一樣,這裡根據這個type區分2.定義HomeListBean實體類裡面巢狀一個BannerInfoBean,因為後面我們要新增標題分組。3.介面卡SectionAdapter裡面就幾個方法convertHead(看這個英文就大概知道頭部,標題的意思也就是分組名),convert這個方法就是item裡面資料綁定了4.HomeFragment這個類裡面請求介面 showData()這個方法是回撥後資料返回的,解析過程就不說了 看上面程式碼,initRedata()這個方法就是就是資料繫結和新增頭部的方法了。好啦講解分析就這麼多,小夥伴們趕緊試試吧!!!