//先贴一下Bean类需要重写MultiItemEntity,这个重写之后会有一个方法,我这里写了一个Index Integer类型的。然后配置一下get和set方法。记得return 返回Index
public class FoodsBeanimplements MultiItemEntity {
private int ret;
private Listdata;
public int getRet() {
return ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public ListgetData() {
return data;
}
public void setData(List data) {
this.data = data;
}
@Override
public int getItemType() {
return 0;
}
public static class DataBeanimplements MultiItemEntity {
/**
* id : 8289
* title : 油焖大虾
* pic : http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg
* collect_num : 1670
* food_str : 大虾 葱 生姜 植物油 料酒
* num : 1670
*/
private Stringid;
private Stringtitle;
private Stringpic;
private Stringcollect_num;
private Stringfood_str;
private int num;
private Integerindex;
public IntegergetIndex() {
return index;
}
public void setIndex(Integer index) {
this.index = index;
}
public StringgetId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public StringgetTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public StringgetPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public StringgetCollect_num() {
return collect_num;
}
public void setCollect_num(String collect_num) {
this.collect_num = collect_num;
}
public StringgetFood_str() {
return food_str;
}
public void setFood_str(String food_str) {
this.food_str = food_str;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public int getItemType() {
return index;
}
}
}
下边是适配器
//我的布局比较简单两个布局一个里边是一个按钮一个里边是一个texView
public class Adapterextends BaseMultiItemQuickAdapter {
public Adapter(@Nullable List data) {
super(data);
//添加自己的多布局
addItemType(0,R.layout.day8_recitem);
addItemType(1,R.layout.day8_recitemmain);
}
@Override
protected void convert(@NotNull BaseViewHolder baseViewHolder, FoodsBean.DataBean dataBean) {
这里的dataBean的getIndex就是刚刚Bean类自己设置的Integer类型。
switch (dataBean.getIndex()){
case 1:
//数据源绑定控件和正常的ViewHolder一样
baseViewHolder.setText(R.id.day8_text,dataBean.getFood_str());
break;
case 0:
baseViewHolder.setText(R.id.day8_btn,dataBean.getTitle());
break;
}
}
}
MainAct 这里解析数据用的是OKGO的get请求
public class Day8Activityextends AppCompatActivity {
private RecyclerViewday8Rec;
private ListmyData=new ArrayList<>();
private Adapteradapter;
int a=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day8);
initView();
day8Rec.setLayoutManager(new LinearLayoutManager(this));
//Get
OkGo.get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1").execute(new StringCallback() {
@Override
public void onSuccess(Response response) {
String string = response.body();
Gson gson=new Gson();
//解析数据
FoodsBean foodsBean = gson.fromJson(string, FoodsBean.class);
myData=foodsBean.getData();
adapter =new Adapter(myData);
day8Rec.setAdapter(adapter);
//这里比较重要我这里设置的a=1;这里和adapter的绑定控件添加数据源有关,同时还设置了要选中的布局是哪一个
for (int i =0; i
myData.get(i).setIndex(a%2);
a++;
}
adapter =new Adapter(myData);
day8Rec.setAdapter(adapter);
}
});
}
private void initView() {
day8Rec = (RecyclerView) findViewById(R.id.day8_rec);
}
}
//最后贴下布局自己比较简单,布局有点丑不要介意
//mainAct
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".day8Exam.Day8Activity">
android:background="@color/colorPrimary"
android:id="@+id/day8_rec"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
//两个ITEM的
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
android:id="@+id/day8_btn"
android:text="发送"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
</androidx.constraintlayout.widget.ConstraintLayout>
//第二个
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/day8_text"
android:text="你好"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
</androidx.constraintlayout.widget.ConstraintLayout>