-
Notifications
You must be signed in to change notification settings - Fork 458
2.个性化配置
KunMinX edited this page May 31, 2022
·
1 revision
该库为一级和二级 Adapter 分别准备了 Config 接口(ILevelPrimaryAdapterConfig
和 ILevelSecondaryAdapterConfig
),自定义配置时,即是去实现这两个接口,来取代默认的配置。
之所以设置成接口的形式,而非 Builder 的形式,是因为二级联动列表内部的联动逻辑需要指明关键的控件。接口相比 Builder 具有强制性,能够让使用者一目了然必须配置的内容,故而采用接口,通过 MVP 架构的方式来编写该库。
关于个性化配置,具体可以参考我在 ElemeGroupedItem
和 SwitchSampleFragment
中编写的案例:
你需要根据需求,在 BaseGroupedItem
的基础上扩展分组实体类,具体的办法是,编写一个实体类,该实体类须继承于 BaseGroupedItem
;该实体类的内部类 ItemInfo
也须继承于 BaseGroupedItem.ItemInfo
。
以 Eleme 分组实体类为例,扩充 content
、imgUrl
、cost
三个字段:
public class ElemeGroupedItem extends BaseGroupedItem<ElemeGroupedItem.ItemInfo> {
public ElemeGroupedItem(boolean isHeader, String header) {
super(isHeader, header);
}
public ElemeGroupedItem(ItemInfo item) { super(item); }
public static class ItemInfo extends BaseGroupedItem.ItemInfo {
private String content;
private String imgUrl;
private String cost;
public ItemInfo(String title, String group, String content) {
super(title, group);
this.content = content;
}
public ItemInfo(String title, String group, String content, String imgUrl) {
this(title, group, content);
this.imgUrl = imgUrl;
}
public ItemInfo(String title, String group, String content, String imgUrl, String cost) {
this(title, group, content, imgUrl);
this.cost = cost;
}
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
public String getImgUrl() { return imgUrl; }
public void setImgUrl(String imgUrl) { this.imgUrl = imgUrl; }
public String getCost() { return cost; }
public void setCost(String cost) { this.cost = cost; }
}
}
注意:如使用 JSON,请在 ProGuard Rules 中为该实体类配置混淆白名单。
在装载数据和实现自定义配置时,泛型框中须指明你编写的实体类,注意 List<ElemeLinkageItem>
,以及 new ILevelSecondaryAdapterConfig<ElemeLinkageItem.ItemInfo>()
这两处。
private void initLinkageDatas(LinkageRecyclerView linkage) {
List<ElemeGroupedItem> items = gson.fromJson(...);
linkage.init(items, new ElemePrimaryAdapterConfig(), new ElemeSecondaryAdapterConfig());
}
private class ElemePrimaryAdapterConfig implements ILinkagePrimaryAdapterConfig {
private Context mContext;
public void setContext(Context context) { mContext = context; }
@Override
public int getLayoutId() { return R.layout.adapter_linkage_primary; }
@Override
public int getGroupTitleViewId() { return R.id.tv_group; }
@Override
public int getRootViewId() { return R.id.layout_group; }
@Override
public void onBindViewHolder(LinkagePrimaryViewHolder holder, boolean selected, String title) {
TextView tvTitle = (TextView) holder.mGroupTitle;
tvTitle.setText(title);
tvTitle.setBackgroundColor(mContext.getResources().getColor(
selected ? R.color.colorPurple : R.color.colorWhite));
tvTitle.setTextColor(ContextCompat.getColor(mContext,
selected ? R.color.colorWhite : R.color.colorGray));
}
}
private class ElemeSecondaryAdapterConfig implements
ILinkageSecondaryAdapterConfig<ElemeGroupedItem.ItemInfo> {
private Context mContext;
public void setContext(Context context) { mContext = context; }
@Override
public int getGridLayoutId() { return 0; }
@Override
public int getLinearLayoutId() { return R.layout.adapter_eleme_secondary_linear; }
@Override
public int getHeaderLayoutId() { return R.layout.adapter_linkage_secondary_header; }
@Override
public int getFooterLayoutId() { return 0; }
@Override
public int getHeaderTextViewId() { return R.id.secondary_header; }
@Override
public int getSpanCountOfGridMode() { return SPAN_COUNT_FOR_GRID_MODE; }
@Override
public void onBindViewHolder(LinkageSecondaryViewHolder holder,
BaseGroupedItem<ElemeGroupedItem.ItemInfo> item) {
((TextView) holder.getView(R.id.iv_goods_name)).setText(item.info.getTitle());
holder.getView(R.id.iv_goods_item).setOnClickListener(v -> {
//TODO
});
}
@Override
public void onBindHeaderViewHolder(LinkageSecondaryHeaderViewHolder holder,
BaseGroupedItem<ElemeGroupedItem.ItemInfo> item) {
((TextView) holder.getView(R.id.secondary_header)).setText(item.header);
}
@Override
public void onBindFooterViewHolder(LinkageSecondaryFooterViewHolder holder,
BaseGroupedItem<DefaultGroupedItem.ItemInfo> item) {
//TODO
}
}