package com.genlot.ninglottery.widget.CustomSpinnerDropDown;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;


import com.genlot.ninglottery.R;

import java.util.List;

/**
 * 类描述：
 * 创建人：songjing
 * 创建时间：2019/6/26 14:03
 * 修改人：songjing
 * 修改时间：2019/6/26 14:03
 * 修改备注：下拉框
 */
public class SpinnerPopWindow extends PopupWindow implements AdapterView.OnItemClickListener {

    private Context mContext;
    private ListView mListView;
    private View baseView;
    private AbstractSpinerAdapter mAdapter;
    private AbstractSpinerAdapter.IOnItemSelectListener mItemSelectListener;


    public SpinnerPopWindow(Context context, View baseView) {
        super(context);
        this.baseView = baseView;
        mContext = context;
        init();
    }


    public void setItemListener(AbstractSpinerAdapter.IOnItemSelectListener listener) {
        mItemSelectListener = listener;
    }


    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_spiner_popwindow, null);
        setContentView(view);
        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00);
        setBackgroundDrawable(dw);
        mListView = view.findViewById(R.id.lv_spiner_item);
        mAdapter = new AbstractSpinerAdapter(mContext);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
        setWidth(baseView.getWidth());
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    }


    public void refreshData(List<String> list) {
        if (list != null) {
            mAdapter.refreshData(list);
            mAdapter.notifyDataSetChanged();
        }
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int pos, long arg3) {
        dismiss();
        if (mItemSelectListener != null) {
            mItemSelectListener.onItemClick(baseView, pos);
        }
    }
}