package com.bjzc.zjyxdxj.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.bjzc.zjyxdxj.R;
import com.bjzc.zjyxdxj.bean.QfdjBean;

import java.util.List;


/**
 * Created by ruihong on 2018/3/28.
 */

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

    private LayoutInflater mLayoutInflater;
    private List<QfdjBean.DataBean> datas;
    private Context mContext;

    private int mSelectedPos = 0;   //实现单选，保存当前选中的position

    private OnChildItemClickListener onChildItemClickListener;


    public QfdjAdapter(Context context, List<QfdjBean.DataBean> data) {
        mLayoutInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.datas = data;
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).isSelected()) {
                mSelectedPos = i;
            }
        }
    }

    public List<QfdjBean.DataBean> getDatas() {
        return datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.item_qfsblist, null);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") final int position) {

        holder.cb.setChecked(datas.get(position).isSelected());

        holder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                datas.get(mSelectedPos).setSelected(false);

                mSelectedPos = position;

                datas.get(mSelectedPos).setSelected(true);

                notifyDataSetChanged();
            }
        });

        holder.tv_smmc.setText(datas.get(position).getDJMC());
        holder.tv_smmc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onChildItemClickListener.onClick(datas.get(position));
            }
        });
    }

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

    public void setOnChildItemClickListener(OnChildItemClickListener onChildItemClickListener) { //接口回调
        this.onChildItemClickListener = onChildItemClickListener;
    }

    public interface OnChildItemClickListener {
        void onClick(QfdjBean.DataBean dataBean);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        CheckBox cb;
        TextView tv_smmc;

        public ViewHolder(View itemView) {
            super(itemView);
            cb = itemView.findViewById(R.id.cb);
            tv_smmc = itemView.findViewById(R.id.tv_smmc);
        }
    }

}
