全部程式碼:
public class BaseBindingiAdapter extends RecyclerView.Adapter<BaseBindingiAdapter.ViewHolder> {
private OnItemListener listener;
private List dataList;
private HashMap<Class, Integer> layoutMap;
private HashMap<Class, Integer> brMap;
private BiMap<Class, Integer> typeMap;
private BaseBindingiAdapter(List dataList, HashMap<Class, Integer> layoutMap, HashMap<Class, Integer> brMap, BiMap<Class, Integer> typeMap, OnItemListener listener) {
this.dataList = new ArrayList();
if (null != dataList) {
this.dataList.addAll(dataList);
}
this.layoutMap = layoutMap;
this.brMap = brMap;
this.typeMap = typeMap;
this.listener = listener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Class clazz = typeMap.inverse().get(viewType);
ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), layoutMap.get(clazz), parent, false);
BaseBindingiAdapter.ViewHolder holder = new BaseBindingiAdapter.ViewHolder(binding.getRoot());
holder.setBinding(binding);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final Object bean = dataList.get(position);
Class clazz = bean.getClass();
holder.getBinding().setVariable(brMap.get(clazz), dataList.get(position));
holder.getBinding().executePendingBindings();
if (null != listener) {
listener.itemListener(bean, holder.getBinding(), position);
}
}
/**
* 重新整理列表的唯一方法,不需要重新呼叫notifyDataSetChanged
*
* @param newDataList
*/
public void setDataList(List newDataList) {
dataList.clear();
if (null != newDataList) {
dataList.addAll(newDataList);
}
notifyDataSetChanged();
}
/**
* 指定索引新增資料
* @param index
* @param addDataList
*/
public void addData(int index, List addDataList) {
if (null != addDataList) {
dataList.addAll(index, addDataList);
notifyDataSetChanged();
}
}
/**
* 新增資料
* @param addDataList
*/
public void addData(List addDataList) {
if (null != addDataList) {
dataList.addAll(addDataList);
notifyDataSetChanged();
}
}
/**
* 獲取資料
* @return
*/
public List getDataList() {
return dataList;
}
@Override
public int getItemViewType(int position) {
Class clazz = dataList.get(position).getClass();
//檢查未配置的型別
if (!typeMap.containsKey(clazz)) {
throw new RuntimeException("DataType:" + clazz.getSimpleName() + " not config!");
}
return typeMap.get(clazz);
}
@Override
public int getItemCount() {
return null == dataList ? 0 : dataList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewDataBinding binding;
public ViewHolder(View itemView) {
super(itemView);
}
public void setBinding(ViewDataBinding binding) {
this.binding = binding;
}
public ViewDataBinding getBinding() {
return this.binding;
}
}
public static class Builder {
private HashMap<Class, Integer> layoutMap = new HashMap<>();
private HashMap<Class, Integer> brMap = new HashMap<>();
private BiMap<Class, Integer> typeMap = HashBiMap.create();
private List listData;
private int viewType = 1;
private OnItemListener listener;
public Builder addType(int layoutId, Class dataType, int br) {
layoutMap.put(dataType, layoutId);
brMap.put(dataType, br);
typeMap.put(dataType, viewType);
viewType++;
return this;
}
public Builder dataList(List listData) {
this.listData = listData;
return this;
}
public Builder addItemListenet(OnItemListener listener) {
this.listener = listener;
return this;
}
public BaseBindingiAdapter create() {
return new BaseBindingiAdapter(listData, layoutMap, brMap, typeMap, listener);
}
}
public interface OnItemListener {
void itemListener(Object bean, ViewDataBinding binding, int position);
}
複製程式碼
}