DataBinding|DataBinding RecyclerView的万能Adapter

RecyclerView万能的Adapter

如果使用了Data Binding Library框架,并使用了RecyclerView.可以使用这个Adapter
不用在为每一个不同的布局写单独的Adapter
功能
  • 添加HeaderView和FooterView
  • Item的Click事件处理
  • 也可以添加额外的逻辑处理
源码地址 https://github.com/Doudada/DataBinding-Librar-RecyclerView-all-purpose-Adapter
使用 RecyclerView.setAdapter
item.xml

DataBindingRecyclerViewAdapter adapter = new DataBindingRecyclerViewAdapter (mContext,R.layout.item,BR.yourVariable,variableList); mRecyclerView.setAdapter(adapter);

setOnItemClick
adapter.setOnItemCkickListener(new DataBindingRecyclerViewAdapter.ItemClickListener(){ @Override public void itemClick(View view, int position) { //.................. } });

addHeader And addFooter
mRecyclerView.setAdapter(adapter); ..... LayoutInflater inflater = LayoutInflater.from(getContext()); View footerView = inflater.inflate(R.layout.recycler_view_load_more,binding.recyclerView,false); adapter.addFooterView(footerView); adapter.notifyDataSetChanged();

LayoutInflater inflater = LayoutInflater.from(getContext()); View footerView = inflater.inflate(R.layout.recycler_view_load_more,binding.recyclerView,false); adapter.addFooterView(footerView); mRecyclerView.setAdapter(adapter);

额外的逻辑处理
如果只给item添加一个variable还不够的话可以另外添加
adapter.setOnBindingViewHolderListener(new DataBindingRecyclerViewAdapter.OnBindingViewHolderListener() { @Override public void onHolderBinding(DataBindingRecyclerViewAdapter.DataBindingViewHolder holder, int position) { ViewDataBinding binding = DataBindingUtil.getBinding(holder.itemView); binding.setVariable(BR.presenter,new MyPresenter()); } });

【DataBinding|DataBinding RecyclerView的万能Adapter】item.xml

    推荐阅读