package com.sipai.jspatrol.MonitorScreen;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sipai.jspatrol.Bean.Nodes;
import com.sipai.jspatrol.R;

import java.util.List;

/**
 * created by lvliming on 2021/3/19
 */
public class MenuTypeAdapter extends RecyclerView.Adapter<MenuTypeAdapter.ViewHolder> {
    private List<Nodes> list;
    private Context context;
    //接口实例
    private RecyclerViewOnItemClickListener onItemClickListener;

    public MenuTypeAdapter(List<Nodes> list, Context context, RecyclerViewOnItemClickListener onItemClickListener) {
        this.list = list;
        this.context = context;
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_monitor_screen,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Nodes item = list.get(position);
        holder.name.setText(item.getText());
        if (item.getSel().equals("sel")){
            holder.name.setTextColor(context.getResources().getColor(R.color.white));
            holder.name.setBackground(context.getResources().getDrawable(R.drawable.bg_layout_shape_type_blue));
        }else {
            holder.name.setTextColor(context.getResources().getColor(R.color.gray));
            holder.name.setBackground(context.getResources().getDrawable(R.drawable.bg_layout_shape_type_white));
        }
        holder.mth.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        View mth;
        TextView name;
        public ViewHolder(View itemView) {
            super(itemView);
            mth = itemView;
            name = (TextView) itemView.findViewById(R.id.moudle_item_monitor_screen_tv_title);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener != null) {
                //注意这里使用getTag方法获取数据
                onItemClickListener.onItemClickListener(v, (Integer) v.getTag());
            }
        }
    }
    //接口回调设置点击事件
    public interface RecyclerViewOnItemClickListener {
        //点击事件
        void onItemClickListener(View view, int position);

    }
}
