MultiTypeAdapter是一款简单好用支持多种类型的RecyclerView的适配器,使用简单,功能强大。
MultiTypeAdapter整个类库只有9个class文件,支持bean和type之间一对多的关系绑定,即一个数据bean对应着一个布局类型。
基本用法
// project build.gradle
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter/' }
google()
jcenter()
}
// module build.gradle
dependencies {
implementation 'me.drakeet.multitype:multitype:3.3.1'
}
代码中的使用
MultiTypeAdapter mAdapter = new MultiTypeAdapter();
//获取数据
HashMap<Class, BaseViewBinder> registerMap = giftScene.registerMessageBinder();
//进行绑定
if (registerMap.size() > 0) {
for (Map.Entry<Class, BaseViewBinder> entry : registerMap.entrySet()) {
mAdapter.register(entry.getKey(), entry.getValue());
}
}
mAdapter.setItems(listMsg);
MiguLinearLayoutManager layoutManager= new MiguLinearLayoutManager(context,
LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(layoutManager);
if (refreshHeader != null && refreshHeader instanceof IRefreshHeader) {
mRecyclerView.setRefreshHeaderView(refreshHeader);
}
//设置adapter
mRecyclerView.setAdapter(mAdapter);
类与布局绑定
//class GiftScene
@Override public HashMap <Class, BaseViewBinder> registerMessageBinder() {
HashMap <Class, BaseViewBinder> registerMap = new HashMap <>();
registerMap.put(GiftListBean.class, new BaseViewBinder(GiftListViewItem.class));
return registerMap;
}
public class BaseViewBinder<V, T> extends ItemViewBinder<V, T, Object> {
public BaseViewBinder() {
}
public BaseViewBinder(Class vuClass) {
this.vuClass = vuClass;
}
public BaseViewBinder(CallBack <Object> callBack) {
itemCallBack = callBack;
}
public BaseViewBinder(Class vuClass, CallBack <Object> callBack) {
this.vuClass = vuClass;
itemCallBack = callBack;
}
@Override
protected void beforeInit() {
super.beforeInit();
}
}
public class GiftListItem extends ViewUtils{
@Override
public int getLayoutId() {
return R.layout.item_view;
}
@Override
public void bindView() {
super.bindView();
//初始化布局
}
@Override
public void bindData() {
super.bindView();
//初始化布局
}
}
快来试一试吧~~