Android Databinding多类型支持 RecyclerView.Adapter

 

使用Data Binding后,RecyclerView.Adapter就变得通用不用去写多个adapter,主要字

​

public class SimpleAdapter<T> extends RecyclerView.Adapter<ViewHolder>{
 
    private List<T> mDatas;
 
    private int layoutId;
 
    private int brId;
 
    public SimpleAdapter(List<T> mDatas, int layoutId, int brId) {
        this.mDatas = mDatas;
        this.layoutId = layoutId;
        this.brId = brId;
    }
 
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        ViewDataBinding binding = DataBindingUtil.inflate(inflater, layoutId, parent, false);
        ViewHolder viewHolder = new ViewHolder(binding.getRoot());
        viewHolder.setBinding(binding);
        return viewHolder;
    }
 
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.getBinding().setVariable(brId,mDatas.get(position));
        holder.getBinding().executePendingBindings();
    }
 
    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

}

​}

但是这样的 Adapter不但不支持多类型,还不支持绑定多个数据,只能绑定一个。如果要实现我们想要的功能就要可以动态获取不同id和动态数据绑定。

  /**
     * 绑定数据的接口
     * @param <T> model
     */
    public interface OnBindDataInterface<T>{
        void onBindData(T model, UniversalViewHolder holder, int type,int position);  //数据绑定
        int getItemLayoutId(int viewType);         //设置布局
    }

    /**
     * item多类型支持
     * @param <T>
     */
    public interface OnMultiTypeBindDataInterface<T> extends OnBindDataInterface<T>{
        int setItemViewType(int position);
    }

    @Override
    public int getItemViewType(int position) {
        if(mOnMultiTypeBindDataInterface != null){
            return mOnMultiTypeBindDataInterface.setItemViewType(position);
        }
        return 0;
    }

adapter中数据绑定就有接口实现

  @Override
    public UniversalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int layoutId = mOnBindDataInterface.getItemLayoutId(viewType);
        UniversalViewHolder holder = UniversalViewHolder.getViewHolder(parent, layoutId);
        return holder;
    }

    @Override
    public void onBindViewHolder(UniversalViewHolder holder, int position) {
        T item = mData.get(position);
        mOnBindDataInterface.onBindData(item, holder, getItemViewType(position),position);

    }

在viewholder中先得到ViewDataBinding,在mOnBindDataInterface再转为自己需要的具体类。这里viewholder的构造方法不支持多类型,就通过static方法后的viewholder,不得引入static的bing,但是这个bing不能直接用。不然数据多了item会重复

public class UniversalViewHolder extends RecyclerView.ViewHolder{

    private static  ViewDataBinding staticBinding;
    private ViewDataBinding binding;
    public  static UniversalViewHolder getViewHolder(ViewGroup parent, int layoutID) {
        staticBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),layoutID,parent,false);
        return new UniversalViewHolder(staticBinding.getRoot());

    }
    public UniversalViewHolder(View itemView) {
        super(itemView);
        binding = staticBinding;
    }
 
    public void setBinding(ViewDataBinding binding) {
        this.binding = binding;
    }

    public ViewDataBinding getBinding() {
        return this.binding;
    }

}

可以看一下我item重复这个bug未发现时候的代码

public class UniversalViewHolder extends RecyclerView.ViewHolder{

    private static  ViewDataBinding binding;
    public  static UniversalViewHolder getViewHolder(ViewGroup parent, int layoutID) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),layoutID,parent,false);
        return new UniversalViewHolder(binding.getRoot());

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

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

    public ViewDataBinding getBinding() {
        return this.binding;
    }

}

最后实现一下接口,这边实现的是单一类型的接口。bing类型转成需要的RecycleItemGoosinfoBinding。

public class GoodsBindData implements UniversalAdapter.OnBindDataInterface<Goods> {
    @Override
    public void onBindData(Goods model, UniversalViewHolder holder, int type, int position) {
        RecycleItemGoosinfoBinding binding = (RecycleItemGoosinfoBinding)  holder.getBinding();
        binding.setGoods(model);
        binding.executePendingBindings();
    }

    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.recycle_item_goosinfo;
    }
}

再看看内部类实现。position可以不传,看个人需求

     UniversalAdapter<String[]> adapter = new UniversalAdapter(dayList, new UniversalAdapter.OnBindDataInterface<String[]>() {
            @Override
            public void onBindData(String[] model, UniversalViewHolder holder, int type,int position) {
                RecycleItemDaydetailBinding binding = (RecycleItemDaydetailBinding)holder.getBinding();
                binding.setDay((position+1 )+"日:  ");
                binding.setDays(model);
            }

            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.recycle_item_daydetail;
            }
        });

希望可以帮助看到这个文章的你

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值