第一步:配置
1.先在 build.gradle 的 repositories 添加:
- allprojects {
- repositories {
- ...
- maven { url "https://jitpack.io" }
- }
- }
2.然后在dependencies添加:
- dependencies {
- compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:v1.5.3'
- }
第二步:解析出来的实体类实现MultiItemEntity
public static class DataBean implements MultiItemEntity {
/**
* uniquekey : 5319327bd71bbcae83beff5cd983f0a6
* title : 周冬雨穿粉色长裙,倪妮穿粉色长裙,李沁穿粉色长裙,全输给了她
* date : 2018-06-21 08:56
* category : 头条
* author_name : 北京时间
* url : http://mini.eastday.com/mobile/180621085631532.html
* thumbnail_pic_s : http://09.imgmini.eastday.com/mobile/20180621/20180621085631_590ada31a08409165b8bfdcbad689c32_2_mwpm_03200403.jpg
* thumbnail_pic_s02 : http://09.imgmini.eastday.com/mobile/20180621/20180621085631_590ada31a08409165b8bfdcbad689c32_1_mwpm_03200403.jpg
* thumbnail_pic_s03 : http://09.imgmini.eastday.com/mobile/20180621/20180621085631_590ada31a08409165b8bfdcbad689c32_4_mwpm_03200403.jpg
*/
private String uniquekey;
private String title;
private String date;
private String category;
private String author_name;
private String url;
private String thumbnail_pic_s;
private String thumbnail_pic_s02;
private String thumbnail_pic_s03;
//定义常量做判断
public static final int CONTENT1=1;
public static final int CONTENT2=2;
public static final int CONTENT3=3;
public String getUniquekey() {
return uniquekey;
}
public void setUniquekey(String uniquekey) {
this.uniquekey = uniquekey;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getAuthor_name() {
return author_name;
}
public void setAuthor_name(String author_name) {
this.author_name = author_name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getThumbnail_pic_s() {
return thumbnail_pic_s;
}
public void setThumbnail_pic_s(String thumbnail_pic_s) {
this.thumbnail_pic_s = thumbnail_pic_s;
}
public String getThumbnail_pic_s02() {
return thumbnail_pic_s02;
}
public void setThumbnail_pic_s02(String thumbnail_pic_s02) {
this.thumbnail_pic_s02 = thumbnail_pic_s02;
}
public String getThumbnail_pic_s03() {
return thumbnail_pic_s03;
}
public void setThumbnail_pic_s03(String thumbnail_pic_s03) {
this.thumbnail_pic_s03 = thumbnail_pic_s03;
}
//重写方法
@Override
public int getItemType() {
//多布局判断一张图片时
if(thumbnail_pic_s02==null&&thumbnail_pic_s03==null){
return 1;
//两张图片时
}else if(thumbnail_pic_s03==null){
return 2;
//三张图片时
}else {
return 3;
}
}
}
}
第三步:适配器
public class MyAdapter extends BaseMultiItemQuickAdapter<Beans.ResultBean.DataBean,BaseViewHolder> {
private Context context;
/**
* Same as QuickAdapter#QuickAdapter(Context,int) but with
* some initialization data.
*
* @param data A new list is created out of this one to avoid mutable list
*/
public MyAdapter(List<Beans.ResultBean.DataBean> data,Context context) {
super(data);
this.context=context;
//R.layout.layou_item1布局
addItemType(Beans.ResultBean.DataBean.CONTENT1,R.layout.layou_item1);
addItemType(Beans.ResultBean.DataBean.CONTENT2,R.layout.layout_item2);
addItemType(Beans.ResultBean.DataBean.CONTENT3,R.layout.layout_item3);
}
@Override
protected void convert(BaseViewHolder helper, Beans.ResultBean.DataBean item) {
switch (helper.getItemViewType()){
case Beans.ResultBean.DataBean.CONTENT1:
helper.setText(R.id.tit1,item.getTitle());
Glide.with(context).load(item.getThumbnail_pic_s()).into((ImageView) helper.getView(R.id.image1));
break;
case Beans.ResultBean.DataBean.CONTENT2:
helper.setText(R.id.title2,item.getTitle());
Glide.with(context).load(item.getThumbnail_pic_s()).into((ImageView) helper.getView(R.id.image2));
Glide.with(context).load(item.getThumbnail_pic_s02()).into((ImageView) helper.getView(R.id.image2_1));
break;
case Beans.ResultBean.DataBean.CONTENT3:
helper.setText(R.id.title3,item.getTitle());
Glide.with(context).load(item.getThumbnail_pic_s()).into((ImageView) helper.getView(R.id.image3));
Glide.with(context).load(item.getThumbnail_pic_s02()).into((ImageView) helper.getView(R.id.image3_1));
Glide.with(context).load(item.getThumbnail_pic_s03()).into((ImageView) helper.getView(R.id.image3_2));
break;
}
}
第四步:XML根据自己的所需布局不详说