ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

RecyclerView系列:RecyclerView中多布局简单实现(一)

2021-05-20 19:01:38  阅读:376  来源: 互联网

标签:系列 String void public ResultBean HomeEntity RecyclerView id 中多


上效果图:

{
    "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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有