阅读 268

Android结合DataBinding封装的BaseBindingAdapter

全部代码:

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);
}
复制代码

}