BRVAH使用指南之多级列表基本使用过程

2019-01-15  本文已影响21人  伽蓝烟雨

框架地址官方基本使用地址: https://www.jianshu.com/p/b343fcff51b0/

1. 自定义Adapter

关于自定义adapter来说,MultiItemEntity为其封装的基本的数据接口类型,也可以理解为标记
将你要加载的list数据转换成List<MultiItemEntity> 类型

addItemType(TYPE_LEVEL_PARENT, R.layout.item_batch_code_group)
最多有多少级就添加多少个type
convert(BaseViewHolder helper, MultiItemEntity item)
根据type生成对应的子级view;

public class BatchCodeAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public static final int TYPE_LEVEL_PARENT = 0;
    public static final int TYPE_LEVEL_CHILD = 1;

    /**
     * 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 BatchCodeAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_LEVEL_PARENT, R.layout.item_batch_code_group);
        addItemType(TYPE_LEVEL_CHILD, R.layout.item_batch_code_child);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        helper.setIsRecyclable(false);
        if (helper.getItemViewType() == TYPE_LEVEL_PARENT) {
            BatchCodeGroupItem batchCodeGroupItem = (BatchCodeGroupItem) item;
            batchCodeGroupItem.setExpanded(true);
            SuperTextView stvItemBindCode = (SuperTextView) helper.getView(R.id.stv_batch_group);
            if (batchCodeGroupItem.getShippingMap().size()==0 ) return;
            stvItemBindCode.setLeftString(batchCodeGroupItem.getShippingMap().get(0).getShippingName()).
                    setRightString("共" + batchCodeGroupItem.getAllNums() + "个包裹,可发" + (batchCodeGroupItem.getAllNums() - batchCodeGroupItem.getCalNums()) + "个").useShape();

        } else {
            BatchCodeGroupItem.ShippingMapBean shippingMapBean = (BatchCodeGroupItem.ShippingMapBean) item;
            SuperTextView stvAutoCancel = (SuperTextView) helper.getView(R.id.stv_item_batch_code_child);
            stvAutoCancel.setRightString(shippingMapBean.getTrackingNumber())
                    .setLeftString(shippingMapBean.getRank()+ "");
            if ("CANCEL".equals(shippingMapBean.getOrderStatus())) { //当订单为取消状态时,设置复制按钮,复制结果为cancel+shipmentId
                stvAutoCancel.setRightTextColor(mContext.getResources().getColor(R.color.red_login_error))
                        .setLeftTextColor(mContext.getResources().getColor(R.color.red_login_error))
                        .useShape();
            }

        }
    }
}

2.数据的实体的继承

BatchCodeGroupItem extends AbstractExpandableItem<子级对象> implements MultiItemEntity
父节点需要继承子级对象,并实现MultiItemEntity 接口
MultiItemEntity有getItemType()方法,标记需与adapter里面的标记一致
AbstractExpandableItem实现了IExpandable接口,里面打的getLevel(),返回的标记最好一致对应
即:下标从0开始。
@Override
public int getItemType() {
return 0;
}
@Override
public int getLevel() {
return 0;
}

public class BatchCodeGroupItem   extends AbstractExpandableItem<BatchCodeGroupItem.ShippingMapBean> implements MultiItemEntity,Serializable {


    @SerializedName("allNums")
    private int allNums;
  
    @SerializedName("shippingMap")
    private List<ShippingMapBean> shippingMap;

    public int getAllNums() {
        return allNums;
    }

    public void setAllNums(int allNums) {
        this.allNums = allNums;
    }





    public List<ShippingMapBean> getShippingMap() {
        return shippingMap;
    }

    public void setShippingMap(List<ShippingMapBean> shippingMap) {
        this.shippingMap = shippingMap;
    }



    @Override
    public int getLevel() {
        return 0;
    }

    @Override
    public int getItemType() {
        return TYPE_LEVEL_PARENT;
    }

    public static class ShippingMapBean implements MultiItemEntity,Serializable {
        @SerializedName("order_status")
        private String orderStatus;
       

        public String getOrderStatus() {
            return orderStatus;
        }

        public void setOrderStatus(String orderStatus) {
            this.orderStatus = orderStatus;
        }

      
      @Override
        public int getItemType() {
          return TYPE_LEVEL_CHILD;
        }
    }
}

3.数据的转换

数据转换时 batchCodeGroupItem.setSubItems()或者循环添加都可以
记住是子元素重新添加到父节点的SubItem中;

public ArrayList<MultiItemEntity> generateData(List<BatchCodeGroupItem> batchCodeGroupItemList) {

      ArrayList<MultiItemEntity> res = new ArrayList<>();
      for (int i = 0; i < batchCodeGroupItemList.size(); i++) {

          BatchCodeGroupItem batchCodeGroupItem = batchCodeGroupItemList.get(i);
          for (int j = 0; j < batchCodeGroupItem.getShippingMap().size(); j++) {
              batchCodeGroupItem.addSubItem(batchCodeGroupItem.getShippingMap().get(j));
          }

          res.add(batchCodeGroupItem);
      }
      return res;
  }

4.绑定数据

默认展开 adapter.expandAll();

  LinearLayoutManager layout = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        ryvBatchPick.setLayoutManager(layout);

        adapter = new BatchCodeAdapter(data);
        ryvBatchPick.setAdapter(adapter);

        adapter.expandAll();

5.加载数据

数据加载完毕,要重新调用一次: adapter.expandAll();

                 adapter.replaceData(data);
                adapter.expandAll();

关键

加载数据要重新调用adapter.expandAll();
数据实体继承要记得加标记

上一篇 下一篇

猜你喜欢

热点阅读