RecyclerView系列:RecyclerView中多布局简单实现(一)
作者:互联网
上效果图:
- 多布局常见于通信软件的聊天页,会显示图片、视频、聊天、日期、头像昵称。
- 引入BaseRecyclerViewAdapterHelper和RecyclerView, 引入Gson。
- 实际开发中,json数据一般是后端返回。这里,自己准备json数据
{
"sale":[
{
"id":"2",
"name":"东风标致308"
}
],
"preSale":[
{
"id":"33",
"autoName":"奇瑞 艾瑞泽3 2015款 1.5L 自动够炫版",
"prePrice":"7.79"
}
]
}
压缩如下:
{"sale":[{"id":"2","name":"东风标致308"}],"preSale":[{"id":"33","autoName":"奇瑞 艾瑞泽3 2015款 1.5L 自动够炫版","prePrice":"7.79"}]}
-
总共需要4个java类
-
使用GsonFormat插件生成ResultBean如下:
public class ResultBean {
private List<SaleBean> sale;
private List<PreSaleBean> preSale;
public List<SaleBean> getSale() {
return sale;
}
public void setSale(List<SaleBean> sale) {
this.sale = sale;
}
public List<PreSaleBean> getPreSale() {
return preSale;
}
public void setPreSale(List<PreSaleBean> preSale) {
this.preSale = preSale;
}
public static class SaleBean {
/**
* id : 2
* name : 东风标致308
*/
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static class PreSaleBean {
/**
* id : 33
* autoName : 奇瑞 艾瑞泽3 2015款 1.5L 自动够炫版
* prePrice : 7.79
*/
private String id;
private String autoName;
private String prePrice;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAutoName() {
return autoName;
}
public void setAutoName(String autoName) {
this.autoName = autoName;
}
public String getPrePrice() {
return prePrice;
}
public void setPrePrice(String prePrice) {
this.prePrice = prePrice;
}
}
}
- 还需生成一个HomeEntity.java,用于传递给Adapter。
HomeEntity
实现了MultiItemEntity
接口的getItemType()方法,如下:
public class HomeEntity implements MultiItemEntity {
public static final int TYPE_SALE = 1;
public static final int TYPE_PRESALE = 2;
private int itemType;
public ResultBean.SaleBean seriesEntity;
public ResultBean.PreSaleBean presaleEntity;
public void setItemType(int itemType) {
this.itemType = itemType;
}
public ResultBean.SaleBean getSeriesEntity() {
return seriesEntity;
}
public void setSeriesEntity(ResultBean.SaleBean seriesEntity) {
this.seriesEntity = seriesEntity;
}
public ResultBean.PreSaleBean getPresaleEntity() {
return presaleEntity;
}
public void setPresaleEntity(ResultBean.PreSaleBean presaleEntity) {
this.presaleEntity = presaleEntity;
}
@Override
public int getItemType() {
return this.itemType;
}
}
- HomeEntity写好后,就开始写Adapter,新建一个
CarSaleAdapter
继承自BaseMultiItemQuickAdapter
,代码如下:
public class CarSaleAdapter extends BaseMultiItemQuickAdapter<HomeEntity, BaseViewHolder> {
public CarSaleAdapter(List<HomeEntity> data) {
super(data);
addItemType(HomeEntity.TYPE_PRESALE, R.layout.layout_presale);
addItemType(HomeEntity.TYPE_SALE, R.layout.layout_sale);
}
@Override
protected void convert(BaseViewHolder helper, HomeEntity item) {
HomeEntity homeEntity = (HomeEntity) item;
switch (homeEntity.getItemType()) {
case HomeEntity.TYPE_PRESALE:
ResultBean.PreSaleBean entity = homeEntity.getPresaleEntity();
helper.setText(R.id.tv_brand_name, entity.getAutoName())
.setText(R.id.tv_lowest_price, entity.getPrePrice());
break;
case HomeEntity.TYPE_SALE:
ResultBean.SaleBean seriesEntity = homeEntity.getSeriesEntity();
helper.setText(R.id.tv_name, seriesEntity.getName());
break;
}
}
}
附上CarSaleAdapter
所需要的布局文件,layout_presale.xml
如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:background="#ff0000">
<TextView
android:id="@+id/tv_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="预售:"
android:textSize="18sp"
/>
<TextView
android:layout_below="@id/tv_tip"
android:id="@+id/tv_brand_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="xx"
android:layout_marginLeft="20dp"
android:textSize="18sp"
/>
<TextView
android:id="@+id/tv_lowest_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textSize="18sp"
android:layout_below="@id/tv_brand_name"
tools:text="yyy"
android:textStyle="bold"
/>
</RelativeLayout>
layout_sale.xml
如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#00ff00">
<TextView
android:id="@+id/tv_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="在售:"
android:textSize="18sp"
/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_tip"
android:layout_marginLeft="20dp"
android:textSize="24sp"
android:textStyle="bold"
tools:text="xxx" />
</RelativeLayout>
- Adapter搞定后,开始测试最终效果:
public class MainActivity extends AppCompatActivity {
String jsonStr = "{\"sale\":[{\"id\":\"2\",\"name\":\"东风标致308\"}],\"preSale\":[{\"id\":\"33\",\"autoName\":\"奇瑞 艾瑞泽3 2015款 1.5L 自动够炫版\",\"prePrice\":\"7.79\"}]}";
ResultBean homeBean;
RecyclerView rvList;
CarSaleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvList = findViewById(R.id.rv_list);
rvList.setLayoutManager(new LinearLayoutManager(this));
homeBean = new Gson().fromJson(jsonStr, ResultBean.class);
List<HomeEntity> list = new ArrayList<>();
for (int i = 0; i < homeBean.getPreSale().size(); i++) {
ResultBean.PreSaleBean preSaleBean = homeBean.getPreSale().get(i);
HomeEntity entity = new HomeEntity();
entity.setItemType(HomeEntity.TYPE_PRESALE);
entity.setPresaleEntity(preSaleBean);
list.add(entity);
}
for (int i = 0; i < homeBean.getSale().size(); i++) {
ResultBean.SaleBean seriesEntity = homeBean.getSale().get(i);
HomeEntity entity = new HomeEntity();
entity.setSeriesEntity(seriesEntity);
entity.setItemType(HomeEntity.TYPE_SALE);
list.add(entity);
}
adapter = new CarSaleAdapter(list);
rvList.setAdapter(adapter);
}
}
标签:系列,String,void,public,ResultBean,HomeEntity,RecyclerView,id,中多 来源: https://blog.csdn.net/zhangjin1120/article/details/117077542