package cn.edu.zstu.sunshine.base;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 适配器的基类
 * Created by CooLoongWu on 2017-8-18 10:50.
 */

public class BaseAdapter<T> extends RecyclerView.Adapter<BaseAdapter.BaseViewHolder> {

    private List<T> data = new ArrayList<>();
    private int br;
    private int layout;
    private OnItemHandler onItemHandler;

    public BaseAdapter(int layout, int br, List<T> data) {
        this.br = br;
        this.data = data;
        this.layout = layout;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewDataBinding viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), layout, parent, false);
        return new BaseViewHolder(viewDataBinding);
    }

    @Override
    public void onBindViewHolder(BaseAdapter.BaseViewHolder holder, int position) {
        holder.getViewDataBinding().setVariable(br, data.get(position));
        holder.getViewDataBinding().executePendingBindings();
        onItemHandler.onItemHandler(holder.getViewDataBinding(), position);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public BaseAdapter setOnItemHandler(OnItemHandler onItemHandler) {
        this.onItemHandler = onItemHandler;
        return this;
    }

    public interface OnItemHandler {
        void onItemHandler(ViewDataBinding viewDataBinding, int position);
    }

    class BaseViewHolder extends RecyclerView.ViewHolder {
        ViewDataBinding viewDataBinding;

        BaseViewHolder(ViewDataBinding viewDataBinding) {
            super(viewDataBinding.getRoot());
            this.viewDataBinding = viewDataBinding;
        }

        ViewDataBinding getViewDataBinding() {
            return viewDataBinding;
        }
    }
}



@@ -1225,24 +1225,65 @@
 Bindings();%0A
+        if (onItemHandler != null) %7B%0A    
         onIt
@@ -1338,32 +1338,42 @@
 g(), position);%0A
+        %7D%0A
     %7D%0A%0A    @Over

修复BaseAdapter一处bug，需要给onItemHandler判空