我以前一直使用BRVAH的adapter,非常好用,搭配databinding后,要是能够在convert中可以直接拿到对应的ViewDataBinding的话,我们的adapter就可以像如下这样
@Override
protected void convert(MVViewHolder<MyBinding> helper, MyEntry item) {
helper.getDataViewBinding().setItem(item);
}
对具体的显示,在xml中完成,就这么一句话就可以完成整个adapter展示内容的绑定了,非常方便
,我们的绑定过程在我们自定义的viewholder中完成,它需要继承BRVAH的BaseViewHolder,贴代码
public class MVViewHolder<T extends ViewDataBinding> extends BaseViewHolder {
T binding = null;
public MVViewHolder(T binding) {
super(binding.getRoot());
binding.getRoot().setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
this.binding = binding;
}
public BaseViewHolder setQAdapter(BaseQuickAdapter adapter) {
super.setAdapter(adapter);
return this;
}
public T getDataViewBinding() {
return this.binding;
}
}
构造函数通过传入对应的viewdatabinding来绑定,其中设置layoutparams是我在解决scrollview嵌套recycleview的过程中,出现展示一行解决的过程中,通过构建binding的时候不传入parent参数出现recycleview的item条目不能充满父控件而写的,大家可以根据自己的情况改动,setQAdapter后面会用,先不用管
接下来是最重要的自定义的adapter,先放代码
public abstract class BaseQAdapter<T, D extends ViewDataBinding, K extends BaseViewHolder> extends BaseQuickAdapter<T, K> {
public BaseQAdapter(int layoutResId, @Nullable List<T> data) {
super(layoutResId, data);
}
public BaseQAdapter(@Nullable List<T> data) {
super(data);
}
public BaseQAdapter(int layoutResId) {
super(layoutResId);
}
@Override
public K onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType != LOADING_VIEW && viewType != HEADER_VIEW && viewType != EMPTY_VIEW && viewType != FOOTER_VIEW) {
D d = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), this.mLayoutResId, null, false);
d.executePendingBindings();
MVViewHolder mvViewHolder = new MVViewHolder(d);
bindViewClickListener(mvViewHolder);
mvViewHolder.setQAdapter(this);
return (K) mvViewHolder;
} else {
return super.onCreateViewHolder(parent, viewType);
}
}
private void bindViewClickListener(final BaseViewHolder baseViewHolder) {
if (baseViewHolder == null) {
return;
}
final View view = baseViewHolder.itemView;
if (view == null) {
return;
}
if (getOnItemClickListener() != null) {
view.setOnClickListener(v -> getOnItemClickListener().onItemClick(BaseQAdapter.this, v, baseViewHolder.getLayoutPosition() - getHeaderLayoutCount()));
}
if (getOnItemLongClickListener() != null) {
view.setOnLongClickListener(v -> getOnItemLongClickListener().onItemLongClick(BaseQAdapter.this, v, baseViewHolder.getLayoutPosition() - getHeaderLayoutCount()));
}
}
}
构造函数和父类一样,没有改变,重要的是onCreateViewHolder中,为了看清楚为什么这样写,我们先看下父类中的onCreateViewHolder是怎么写的
@Override
public K onCreateViewHolder(ViewGroup parent, int viewType) {
K baseViewHolder = null;
this.mContext = parent.getContext();
this.mLayoutInflater = LayoutInflater.from(mContext);
switch (viewType) {
case LOADING_VIEW:
baseViewHolder = getLoadingView(parent);
break;
case HEADER_VIEW:
baseViewHolder = createBaseViewHolder(mHeaderLayout);
break;
case EMPTY_VIEW:
baseViewHolder = createBaseViewHolder(mEmptyLayout);
break;
case FOOTER_VIEW:
baseViewHolder = createBaseViewHolder(mFooterLayout);
break;
default:
baseViewHolder = onCreateDefViewHolder(parent, viewType);
bindViewClickListener(baseViewHolder);
}
baseViewHolder.setAdapter(this);
return baseViewHolder;
}
可以看出,在BRVAH中的view ,主要包含的几种类型,LOADING_VIEW,HEADER_VIEW,EMPTY_VIEW,FOOTER_VIEW, 分别在这里创建了viewholder,而我们关心的仅仅只是当中我们自己的view,因此在其他的地方,我返回的是super,接下来就是通过构造把我们的binding传进去,并把adapter以及一些点击事件绑定,记得一定要绑定,不绑定点击事件的话点击事件无效,不绑定adapter的话会导致父类其他地方通过adapter找view的时候,出现空指针,弄好后看下超级简单的具体用法
新建xml item_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="name"
type="String"></variable>
</data>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{name}" />
</FrameLayout>
</layout>
adapter
public class MyDemoAdapter extends BaseQAdapter<String, ItemDemoBinding, MVViewHolder<ItemDemoBinding>> {
public MyDemoAdapter(@Nullable List<String> data) {
super(R.layout.item_demo, data);
}
@Override
protected void convert(MVViewHolder<ItemDemoBinding> helper, String item) {
helper.getDataViewBinding().setName(item);
helper.addOnClickListener(R.id.name);
}
}
再搭配databinding的属性设置,看下我们的recycleview
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:bindLayoutManager="@{manager}"
android:loadMoreEnable="@{true}"
android:onChildItemClick="@{vm::OnItemChildClick"
android:onLoadMore="@{vm::onLoadMore}"
app:adapter="@{adapter}"></android.support.v7.widget.RecyclerView>
这样就基本完成了,当然,这只是对basequickadapter的一个写法,brvah的其他功能adapter也可以按着这个思路写就好了
网友评论