package com.philisense.rdgo.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.philisense.rdgo.R;
import com.philisense.rdgo.databinding.ItemOilBinding;

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

/**
 * Created by shaop on 2018/1/5.
 */

public class HqtOilAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<String> dataArray;
    private Context context;

    public HqtOilAdapter(Context context) {
        this.context = context;
        dataArray = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemOilBinding mBindHeader = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_oil, parent, false);
        return new ViewHolder(mBindHeader.getRoot());
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder lineViewHolder = (ViewHolder) holder;
        lineViewHolder.bindView(dataArray.get(position));

        lineViewHolder.itemOilBinding.detel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                monItemClickListener.btnOnClick(dataArray.get(position), position);
            }
        });

    }



    public interface OnItemClickListener {
        void btnOnClick(String s, int postion);
    }

    public OnItemClickListener monItemClickListener;

    public void setOnClickListener(OnItemClickListener onClickListener) {
        this.monItemClickListener = onClickListener;
    }
    @Override
    public int getItemCount() {
        return dataArray.size();
    }

    /**
     * 添加数据
     *
     * @param data 数据
     */
    public void insertData(List<String> data) {

    }

    /**
     * 初始化数据
     *
     * @param data 数据
     */
    public void setData(List<String> data) {
        dataArray = data;
        notifyDataSetChanged();
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        ItemOilBinding itemOilBinding;

        ViewHolder(View view) {
            super(view);
            itemOilBinding = DataBindingUtil.getBinding(view);
        }

        public void bindView(String dataBean) {
            itemOilBinding.setName(dataBean);
        }
    }
}

