-
Notifications
You must be signed in to change notification settings - Fork 1
RecyclerViewAdapter
arjinmc edited this page Jul 23, 2020
·
5 revisions
An adapter extends RecyclerView.Adapter and has two contructors.viewType>=0. ViewType of items will be automatic grown to 0~n according to the layout resouce array "int[] typeLayoutIds".
继承RecyclerView.Adapter的adapter,有两个构造方法。viewType>=0.ViewType会根据布局资源数组"int[] typeLayoutIds"自增加从0到n。
This adapter has data cache that you need to call "notifyDataChanged(List dataList)“ to udpate the data cache when the data has been changed.Other notifyItemXXXX methods from original RecyclerView.Adapter, in this adapter you should use notifyDataItemXXXX instead.
这个adapter是有数据缓存的,如果当数据发生了变化,你需要使用"notifyDataChanged(List dataList)“来更新数据。原RecyclerView.Adapter的其他notifyItemXXXX方法,在此adapter中需要用notifyDataItemXXXX来代替。
- RecyclerViewAdapter(Context context, List dataList, @LayoutRes int layoutId, RecyclerViewSingleTypeProcessor singleTypeProcessor)
RecyclerViewAdapter adapter = new RecyclerViewAdapter<>(this, Arrays.asList(titles)
, R.layout.item_main_list
, new RecyclerViewSingleTypeProcessor<String>() {
@Override
public void onBindViewHolder(RecyclerViewViewHolder holder, final int position, String str) {
TextView textView = holder.getView(R.id.tv_content);
textView.setText(str);
}
});
- RecyclerViewAdapter(Context context, List dataList, @LayoutRes int[] typeLayoutIds, RecyclerViewMultipleTypeProcessor multipleTypeProcessor)
mAdapter = new RecyclerViewAdapter<>(this, mDataList
, new int[]{R.layout.item_list_type0, R.layout.item_list_type1}
, new RecyclerViewMultipleTypeProcessor<Car>() {
@Override
public void onBindViewHolder(RecyclerViewViewHolder holder, int position, Car object) {
if(getItemViewType(position) == 0) {
TextView textView = holder.getView(R.id.tv_content);
textView.setText(object.getBrand() + "/" + object.getTypeName());
}else{
}
}
@Override
public int getItemViewType(int position) {
//define two viewTypes
if (position % 2 == 0)
return 1;
return 0;
}
});