美文网首页架构DatabindingAndroid知识
打造基于Databinding与RecyclerView的通用A

打造基于Databinding与RecyclerView的通用A

作者: 码农明明 | 来源:发表于2017-02-19 22:52 被阅读1551次

    一 想法产生

    做Android开发的童鞋都知道,展示一个列表页面需要以下几个步骤:
    - 创建一个ListView或RecyclerView

    • 创建Item布局
    • 创建Adapter
    • 使用:将ListView与item关联起来
      按照以上做法,有多少列表,就有多少个Adapter。而这些adapter中有很多相似的方法,例如列表的初始化、添加、删除、更新。。。这些步骤完全就是重复的,而且这样做,很多业务逻辑要移到Adapter类中,显得比较乱。因此便诞生了这样一个想法。

    二 Adapter实现思路与事件(使用RecyclerView )

    数据: 数据可以使用泛型

    private List<T1> list
    

    构造函数: Adapter需要绑定Item布局,可以在构造函数中将布局作为参数传进去。添加@LayoutRes注解指明布局资源。

     public BaseAdapter(Context context, @LayoutRes int layout){
      inflater=LayoutInflater.from(context);
      this.context=context;
      this.layout= layout;
      list=new ArrayList<>();
    
    }
    
      重写父方法:onCreateViewHolder、getItemCount
    
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view = inflater.inflate(layout,parent,false);
      return new BaseViewHolder(view);
    }
    @Override
    public int getItemCount() {
      return list.size();
    }
    

    ViewHolder创建:生成ViewDataBinding并提供get方法(注意不要漏掉static)

    public static class BaseViewHolder extends RecyclerView.ViewHolder {
      private ViewDataBinding b;
      public BaseViewHolder(View itemView) {
      super(itemView);
      b=DataBindingUtil.bind(itemView);
      }
      public ViewDataBinding getBinding() {
      return b;
      }
    }
    

    回调接口:用于ViewDataBinding的回调,并在onBindViewHolder中将得到的holder与position回调出去

    public interface BindView<T2> {
      void onBindViewHolder(T2 b,int position);
    }
    
    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
      bindView.onBindViewHolder((T2) holder.getBinding(),position);
    }
    

    使用:为adapter创建回调。

    adapter.setOnBindViewHolder(new BaseAdapter.BindView<ItemActionBinding>() {
      @Override
      public void onBindViewHolder(ItemActionBinding b, int position) {
      b.setAction(actionInfos.get(position));
      }
    });
    

    三 Adapter全部代码

    package com.sunmoon.helper.adapter;
    
    import android.content.Context;
    import android.databinding.DataBindingUtil;
    import android.databinding.ViewDataBinding;
    import android.support.annotation.LayoutRes;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class BaseAdapter<T1, T2 extends ViewDataBinding> extends RecyclerView.Adapter<BaseAdapter.BaseViewHolder> {
        private List<T1> list;
        private LayoutInflater inflater;
        private Context context;
        @LayoutRes
        private int layout;
        private BindView<T2> bindView;
    
        public BaseAdapter(Context context, @LayoutRes int layout) {
            inflater = LayoutInflater.from(context);
            this.context = context;
            this.layout = layout;
            list = new ArrayList<>();
    
        }
    
        public void initList(List<T1> list) {
            this.list = list;
        }
    
        public void setOnBindViewHolder(BindView<T2> bindView) {
            this.bindView = bindView;
        }
    
        @Override
        public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = inflater.inflate(layout, parent, false);
            return new BaseViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(BaseViewHolder holder, int position) {
            bindView.onBindViewHolder((T2) holder.getBinding(), position);
        }
    
        @Override
        public int getItemCount() {
            return list.size();
        }
    
        public interface BindView<T2> {
            void onBindViewHolder(T2 b, int position);
        }
    
        public static class BaseViewHolder extends RecyclerView.ViewHolder {
            private ViewDataBinding b;
    
            public BaseViewHolder(View itemView) {
                super(itemView);
                b = DataBindingUtil.bind(itemView);
            }
    
            public ViewDataBinding getBinding() {
                return b;
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:打造基于Databinding与RecyclerView的通用A

        本文链接:https://www.haomeiwen.com/subject/ywxswttx.html