RecycelrViewAdapter的封装
It’s well known that RecyclerView is a powerful tool to create a list view or a grid view, however, it’s also complicated to use.
There’s a passage I writed a few months ago, in this passage I used an example to show the basic usage of RecyclerView.
—>RecyclerView基础用法
So in my opinion, there are six steps to use RecyclerView.
- Add dependecy of RecyclerView
- Create an activity/fragment, and use the control in it’s layout file.
- Create an item, which is the model of your every list’s view.
- Create a class as a model, which contains the information that is needed in every item.
- Create an adapter.
- Use it in your activity or fragment.
The most important step is step 5, an adapter is so complicated to create. So how can we solve the problem?
There is a princple in Java: High cohesion, low coupling,so we would better to separate the viewHolder from RecyclerViewAdapter
So first, we can create a base adapter.
BaseRecyclerViewAdapter
1 | public abstract class BaseRecyclerViewAdapter<Data> extends RecyclerView.Adapter<BaseViewHolder> { |
So We just look at the basic ones.
It’s obviou that in the class we override the three classic methods–onCreateViewHolder(),onBindViewHolder(),getItemCount();
First, we create a ViewHolder1
2
3
4
5
6
("unchecked")
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(mItemLayoutId, parent, false);
return new BaseViewHolder(mContext, view);
}
Then we will bind the holder to the data1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void onBindViewHolder(BaseViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v, mDataList.get(position), position);
}
}
});
bindView(holder, mDataList.get(position));
}
we also set clicklistener to the item
We don’t directly bind the data to the view because we don’t exactly know how many data,how many controls need to be binded, so we should define an abstract method to be override in different situation. That’s why we use the bindView().
So we just need to override two methods–the constuction methond of your class and the bindView() if we want to use it in subclass.
The getItemCount() is so easy that we don’t talk about it.
BaseViewHolder
1 |
|
The annotate is so clear, from the code of BaseRecyclerViewAdatper we can know that if we want to use the methond bindView(), we should put the parameters of viewHolder which is the type of BaseViewHolder, so in the method, we can use the methods of viewHolder such as setTxt(), setImageRes() to bind the controls the view.1
2
3
4
5
6
7/**
* 绑定item中的view和数据
*
* @param viewHolder
* @param item
*/
protected abstract void bindView(BaseViewHolder viewHolder, Data item);
So how easy the way to use RecyclerViewAdapter can be if we use the BaseRecyclerViewAdapter? Don’t worry, there is an example1
2
3
4
5
6
7
8
9
10
11
12
13
14public class CantDoListAdapter extends BaseRecyclerViewAdapter<CalendarModel.BadBean> {
public CantDoListAdapter(List<CalendarModel.BadBean> badBeanList, Context context, int itemLayoutId) {
super(badBeanList, context, itemLayoutId);
}
protected void bindView(BaseViewHolder viewHolder, CalendarModel.BadBean item) {
viewHolder.setText(R.id.item_calendar_can_do,item.getTitle())
.setText(R.id.item_calendar_can_do_detail,item.getDescription());
ImageView imageView = (ImageView)viewHolder.getItemView().findViewById(R.id.item_image_flag);
imageView.setBackgroundColor(mContext.getResources().getColor(R.color.red));
}
}
Is so easy, isn’t it?