package com.bonait.bnframework.modules.home.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE;
import com.bonait.bnframework.common.helper.I.MyClickListener;

import java.util.List;

public class gnleft_adapter extends ArrayAdapter<String> {
    /**
     * 内部点击事件
     */
    private MyClickListener mListener;
    private List<String> datas;

    private int selectedPosition = -1;// 选中的位置
    int resource1;

    public gnleft_adapter(@NonNull Context context, int resource, @NonNull List<String> objects, MyClickListener listener) {
        super(context, resource, objects);
        mListener = listener;
        datas = objects;
        this.resource1 = resource;
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        String lc = (String) getItem(position);//得到当前项选中item实例
        //为每一个子项加载设定的布局
        View view = LayoutInflater.from(getContext()).inflate(resource1, parent, false);
        //分别获取 image view 和 textview 的实例
        RelativeLayout layout=view.findViewById(R.id.tv_type_layout);
        RelativeLayout image=view.findViewById(R.id.tv_image);
        TextView text = view.findViewById(R.id.tv_type);
        text.setText(lc+"");

        if (selectedPosition == position) {
            layout.setBackground(getContext().getResources().getDrawable(R.color.main_background));
            image.setVisibility(View.VISIBLE);
            text.setTextColor(getContext().getResources().getColor(R.color.app_color_blue));
        } else {
            layout.setBackground(getContext().getResources().getDrawable(R.color.white));
            image.setVisibility(View.GONE);
            text.setTextColor(getContext().getResources().getColor(R.color.tab_text_normal) );
        }
        return view;
    }
}
