相信很多小伙伴之前用过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 {
public HomeListBean(boolean isHeader, String header) {
super(isHeader, header);
}
public HomeListBean(BannerInfoBean bannerInfoBean) {
super(bannerInfoBean);
}
private String bannerType;
private String title;
private List 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 getBannerInfo() {
return bannerInfo;
}
public void setBannerInfo(List 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 {
private Context context;
public SectionAdapter(int layoutResId, int sectionHeadResId, List 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) 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()这个方法就是就是数据绑定和添加头部的方法了。好啦讲解分析就这么多,小伙伴们赶紧试试吧!!!