1package com.android.example.bindingdemo; 2 3import android.support.v7.widget.RecyclerView; 4import android.view.LayoutInflater; 5import android.view.ViewGroup; 6 7import android.databinding.DataBindingUtil; 8import android.databinding.ViewDataBinding; 9 10abstract public class DataBoundAdapter<T extends ViewDataBinding> 11 extends RecyclerView.Adapter<DataBoundAdapter.DataBoundViewHolder<T>> { 12 final int mLayoutId; 13 final Class<T> mBinderInterface; 14 public DataBoundAdapter(int mLayoutId, Class<T> mBinderInterface) { 15 this.mLayoutId = mLayoutId; 16 this.mBinderInterface = mBinderInterface; 17 } 18 19 @Override 20 public DataBoundAdapter.DataBoundViewHolder<T> onCreateViewHolder(ViewGroup viewGroup, 21 int type) { 22 T binder = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), mLayoutId, 23 viewGroup, false); 24 return new DataBoundViewHolder(binder); 25 } 26 27 static class DataBoundViewHolder<T extends ViewDataBinding> extends RecyclerView.ViewHolder { 28 public final T dataBinder; 29 public DataBoundViewHolder(T mViewBinder) { 30 super(mViewBinder.getRoot()); 31 this.dataBinder = mViewBinder; 32 } 33 } 34} 35