当RecycleView遇上DataBinding

想让RecycleView的代码更简洁吗? 那就使用DataBinding吧~~

DataBinding可以直接将数据和View绑定 具体可以看一下layout文件
recycleView的layout





item的layout


    









那DataBinding怎么和RecycleView.Adapter 中使用呢? 看一下adapter和ViewHolder中的代码
重点是在 onCreateViewHolder函数中 初始化ViewDataBinding相当于初始化view,将初始化后的ViewDataBinding放置在ViewHolder中方便在 onBindViewHolder 函数中使用 setStu 或 setVariable 绑定数据
@Override
public StuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    StuItemLayoutBinding itemLayoutBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.stu_item_layout,parent,false);
    StuViewHolder viewHolder = new StuViewHolder(itemLayoutBinding.getRoot());
    viewHolder.setBinding(itemLayoutBinding);
    return viewHolder;
}


@Override
public void onBindViewHolder(StuViewHolder holder, int position) {
    StuBean stuBean = mStuList.get(position);
    holder.getBinding().setStu(stuBean);
    //holder.getBinding().setVariable(BR.stu,stuBean);
    holder.getBinding().executePendingBindings();
}

@Override
public int getItemCount() {
    return mStuList == null ?0:mStuList.size();
}
使用了DataBinding后ViewHolder 就不用使用findViewById ,但是需要提供ViewDataBinding对象方便后面绑定数据
class StuViewHolder extends RecyclerView.ViewHolder{

private StuItemLayoutBinding binding;

public StuViewHolder(View itemView) {
    super(itemView);
}

public void setBinding(StuItemLayoutBinding stuItemLayoutBinding){
    this.binding = stuItemLayoutBinding;
}

public StuItemLayoutBinding getBinding() {
    return binding;
}
}
接着可以给RecycleView setAdapter了
   dataBinding.stuRecycleView.setLayoutManager(new LinearLayoutManager(this));
   dataBinding.stuRecycleView.setAdapter(new StuAdapter(stuList));
再想一下,在这个基础上是不是可以将变量抽取出来 用泛型打造一个通用的Adapter
public class BaseRecycleAdapter extends RecyclerView.Adapter{
private List mList;
private int itemLayoutId;
private int variableId;

public BaseRecycleAdapter(List mList, int itemLayoutId, int variableId) {
    this.mList = mList;
    this.itemLayoutId = itemLayoutId;
    this.variableId = variableId;
}

@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    T itemDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),itemLayoutId,parent,false);
    BaseViewHolder viewHolder = new BaseViewHolder(itemDataBinding.getRoot());
    viewHolder.setDataBinding(itemDataBinding);
    return viewHolder;
}

@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
    E data = mList.get(position);
    holder.getDataBinding().setVariable(variableId,data);
    holder.getDataBinding().executePendingBindings();
}

@Override
public int getItemCount() {
    return mList == null ? 0 : mList.size();
}

public class BaseViewHolder  extends RecyclerView.ViewHolder{

    private T dataBinding;

    public BaseViewHolder(View itemView) {
        super(itemView);
    }

    public void setDataBinding(T dataBindding){
        this.dataBinding = dataBindding;
    }

    public T getDataBinding(){
        return dataBinding;
    }
}

}
这样在使用时就更方便了
 dataBinding.stuRecycleView.setLayoutManager(new LinearLayoutManager(this));
 dataBinding.stuRecycleView.setAdapter(new BaseRecycleAdapter(stuList,R.layout.stu_item_layout,BR.stu));
总结:以上只是DataBinding和RecycleView的简单使用,但对我们的代码结构还是有很大帮助的。

你可能感兴趣的:(当RecycleView遇上DataBinding)