实现RecycleView多布局BaseRecyclerViewAdapterHelper

阅读 2
在开发过程中我们会碰到一个大接口返回了多种数据类型的list, 这时候我们首先想到的时使用一个recycleview, 如果是使用addHeader方式进行添加一个新的list, item并不能得到复用, recycleview的性能并没有得到很好的利用, 使用BaseRecyclerViewAdapterHelper, adapter继承BaseMultiItemQuickAdapter,根据不同的type类型, 添加不同的样式的view.
{"series":
    [
        {
            "id":"2", // 车系id
            "name":"东风标致308", // 车系名
        }
    ],
    "preSale": // 预售
    [
         {
             "id":"33",
             "autoName": "奇瑞 艾瑞泽3 2015款 1.5L 自动够炫版",
             "prePrice": "7.79", // 预售价
         }    
]}

分析: 这个接口中返回的是两个List, 但是数据结构完全不同, 需要在同一个页面展示, 我们需要使用一个recycleview是性能达到最大优化的展示.

  • 首先要集成[BRVAH] 参考资料链接: (https://link.jianshu.com/?t=https://github.com/CymChad/BaseRecyclerViewAdapterHelper)
  • 然后我们需要创建一个实体类, 实现接口MultiItemEntity, 定义两种itemType,并且添加两个集合的bean对象

public class HomeEntity implements MultiItemEntity {

  public static final int TYPE_SERIES = 1;
  public static final int TYPE_PESALE = 2;
  private int itemType;

  public SeriesEntity seriesEntity;
  public PresaleEntity presaleEntity;

  public SeriesEntity getSeriesEntity() {
      return seriesEntity;
  }

  public void setSeriesEntity(SeriesEntity seriesEntity) {
      this.seriesEntity = seriesEntity;
  }

  public PresaleEntity getPresaleEntity() {
      return presaleEntity;
  }

  public void setPresaleEntity(PresaleEntity presaleEntity) {
      this.presaleEntity = presaleEntity;
  }
  @Override
  public int getItemType() {
      return this.itemType;
  }

  public class SeriesEntity {

      private String id;
      private String name;

      public SeriesEntity(String id, String name) {
          this.id = id;
          this.name = name;
      }

      public String getId() {
          return id;
      }

      public void setId(String id) {
          this.id = id;
      }
}

  public class PresaleEntity {
      public PresaleEntity(String id, String autoName, String prePrice) {
          this.id = id;
          this.autoName = autoName;
          this.prePrice = prePrice;
      }

      private String id;
      private String autoName;
      private String prePrice;

      public String getId() {
          return id;
      }

      public void setId(String id) {
          this.id = id;
      }
  }
}​
创建adapter , 继承BaseMultiItemQuickAdapter, 需要在adapter的构造方法中, 添加type和对应的布局样式, 在convert方法中, 根据item的type类型对不同布局进行操作.

public class MultiAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public MultiAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(HomeEntity.TYPE_PESALE, R.layout.layout_presale);
        addItemType(HomeEntity.TYPE_SERIES, R.layout.layout_series);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        HomeEntity homeEntity = (HomeEntity) item;
        switch (homeEntity.getItemType()) {
            case HomeEntity.TYPE_PESALE:
                HomeEntity.PresaleEntity entity = homeEntity.getPresaleEntity();
                helper.setText(R.id.tv_brand_name, entity.getAutoName())
                        .setText(R.id.tv_lowest_price, entity.getPrePrice());
            case HomeEntity.TYPE_SERIES:
                HomeEntity.SeriesEntity seriesEntity = homeEntity.getSeriesEntity();
                helper.setText(R.id.tv_name, seriesEntity.getName());
        }
    }
}​

最后一步,只需要对adapter设置数据就好了~~

//通过接口拿到数据实体类, homebean, 然后将homebean中的元素转化为homeEntity, 加到list集合中
        List<HomeEntity> list = new ArrayList<>();
        for (int i = 0; i < homeBean.getPreSale().size(); i++) {
            HomeEntity.PresaleEntity preSaleBean = homeBean.getPreSale().get(i);
            HomeEntity entity = new HomeEntity(HomeEntity.TYPE_PESALE);
            entity.setPresaleEntity(preSaleBean);
            list.add(entity);
        }
        for (int i = 0; i < homeBean.getSales().size(); i++) {
            HomeEntity.SeriesEntity seriesEntity = homeBean.getSales().get(i);
            HomeEntity entity = new HomeEntity(HomeEntity.TYPE_SERIES);
            entity.setSeriesEntity(seriesEntity);
            list.add(entity);
        }
    mAdapter.setNewData(list);
原文:https://www.jianshu.com/p/096b7a9ca95a
各位看官,如果你觉得本文不错,请支持一下~