框架地址官方基本使用地址: http://www.reibang.com/p/b343fcff51b0/
1. 自定義Adapter
關(guān)于自定義adapter來(lái)說(shuō)话浇,MultiItemEntity為其封裝的基本的數(shù)據(jù)接口類型,也可以理解為標(biāo)記
將你要加載的list數(shù)據(jù)轉(zhuǎn)換成List<MultiItemEntity> 類型
addItemType(TYPE_LEVEL_PARENT, R.layout.item_batch_code_group)
最多有多少級(jí)就添加多少個(gè)type
convert(BaseViewHolder helper, MultiItemEntity item)
根據(jù)type生成對(duì)應(yīng)的子級(jí)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() + "個(gè)包裹,可發(fā)" + (batchCodeGroupItem.getAllNums() - batchCodeGroupItem.getCalNums()) + "個(gè)").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())) { //當(dāng)訂單為取消狀態(tài)時(shí)午乓,設(shè)置復(fù)制按鈕磅摹,復(fù)制結(jié)果為cancel+shipmentId
stvAutoCancel.setRightTextColor(mContext.getResources().getColor(R.color.red_login_error))
.setLeftTextColor(mContext.getResources().getColor(R.color.red_login_error))
.useShape();
}
}
}
}
2.數(shù)據(jù)的實(shí)體的繼承
BatchCodeGroupItem extends AbstractExpandableItem<子級(jí)對(duì)象> implements MultiItemEntity
父節(jié)點(diǎn)需要繼承子級(jí)對(duì)象沪铭,并實(shí)現(xiàn)MultiItemEntity 接口
MultiItemEntity有g(shù)etItemType()方法,標(biāo)記需與adapter里面的標(biāo)記一致
AbstractExpandableItem實(shí)現(xiàn)了IExpandable接口偏瓤,里面打的getLevel(),返回的標(biāo)記最好一致對(duì)應(yīng)
即:下標(biāo)從0開(kāi)始杀怠。
@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.數(shù)據(jù)的轉(zhuǎn)換
數(shù)據(jù)轉(zhuǎn)換時(shí) batchCodeGroupItem.setSubItems()或者循環(huán)添加都可以
記住是子元素重新添加到父節(jié)點(diǎn)的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.綁定數(shù)據(jù)
默認(rèn)展開(kāi) adapter.expandAll();
LinearLayoutManager layout = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
ryvBatchPick.setLayoutManager(layout);
adapter = new BatchCodeAdapter(data);
ryvBatchPick.setAdapter(adapter);
adapter.expandAll();
5.加載數(shù)據(jù)
數(shù)據(jù)加載完畢厅克,要重新調(diào)用一次: adapter.expandAll();
adapter.replaceData(data);
adapter.expandAll();
關(guān)鍵
加載數(shù)據(jù)要重新調(diào)用adapter.expandAll();
數(shù)據(jù)實(shí)體繼承要記得加標(biāo)記