In line with what is written in this article Android RecylerView multi layout optimization Extract the MyAdapter and write a jacenrecylerview Adapter. It can basically realize some simple pages without writing so many adapters.
Don't say much, post code directly.
/**
* For the time being, I'd like to integrate multiple layouts into one adapter one day
* @version 1.0
* @email jacen@iswsc.com
* Created by Jacen on 2017/12/31 0:51.
*/
public class JacenRecylerViewAdapter<T> extends RecyclerView.Adapter {
protected Context context;
protected OnItemClickListener l;
protected List<T> mList;
private SparseArray<IViewItem> sparseArray;
/**
* If it is a single layout, sparseArray.put(0,IViewItemImpl);
* Otherwise, copy the key corresponding to the layout corresponding to getItemViewType(int position) method
* @param context
* @param l
* @param mList
* @param sparseArray
*/
public JacenRecylerViewAdapter(Context context, OnItemClickListener l, List<T> mList, SparseArray<IViewItem> sparseArray) {
this.context = context;
this.l = l;
this.mList = mList;
this.sparseArray = sparseArray;
}
public void updateList(List<T> mList) {
this.mList = mList;
notifyDataSetChanged();
}
public void addData(T data,int position){
if(mList == null){
mList = new ArrayList<T>();
}
mList.add(position,data);
notifyItemInserted(position);
notifyItemChanged(position);
}
public void updateData(T data, int position){
if(mList == null) return;
mList.remove(position);
mList.add(position,data);
}
public T getData(int position){
return mList != null ? mList.get(position) : null ;
}
public void removeData(int position){
mList.remove(position);
notifyItemRemoved(position);
notifyItemChanged(position);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return sparseArray.get(viewType).onCreateViewHolder(context,parent,l);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
sparseArray.get(getItemViewType(position)).onBindViewHolder(holder,mList.get(position));
}
@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
}
It's OK to write in Activity
SparseArray<IViewItem> sparseArray = new SparseArray();
sparseArray.put(ChatType.text.getLeft(),new ChatLeftTextViewItemImpl());
sparseArray.put(ChatType.text.getRight(),new ChatRightTextViewItemImpl());
//This is for multiple layouts. If it's a single layout, it's OK
//sparseArray.put(0,new ChatLeftTextViewItemImpl());
//Because getItemViewType in the adapter source code returns 0 by default
mAdapter = new MyAdapter(this, mList,sparseArray, this);
Because it's multi layout inheritance, jacenrecylerview adapter overrides getItemViewType
/**
* @version 1.0
* @email jacen@iswsc.com
* Created by Jacen on 2017/12/31 1:55.
*/
public class MyAdapter extends JacenRecylerViewAdapter {
/**
* If it is a single layout, sparseArray.put(0,IViewItemImpl);
* Otherwise, copy the key corresponding to the layout corresponding to getItemViewType(int position) method
*
* @param context
* @param l
* @param mList
* @param sparseArray
*/
public MyAdapter(Context context, List mList, SparseArray sparseArray,OnItemClickListener l) {
super(context, l, mList, sparseArray);
}
@Override
public int getItemViewType(int position) {
ChatMessageVo vo = (ChatMessageVo) mList.get(position);
return vo.isMe() ? vo.getChatType().getRight() : vo.getChatType().getLeft();
}
}
It's done!
If it's not a good place, welcome to reply