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
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量