使用PopupWindow实现的自定义下拉列表

其中的SpinnerAdapter可以在上一篇博客”定义属于自己的适配器“中看到

自定义属于自己的PopupWindow

public class SpinnerPopWindow extends PopupWindow implements AdapterView.OnItemClickListener {

    private ListView mListView;
    private Context mContext;
    private MItemSelectListener mItemSelectListener;
    private MyAdapter myAdapter;

    public SpinnerPopWindow(Context context) {
        super(context);

        this.mContext = context;
        init();
    }

    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.spinner_popwindow, null);
        setContentView(view);
        mListView = (ListView) view.findViewById(R.id.spinner_list);

        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        setFocusable(true);//设置为true的时候才能点击列表中的item
        ColorDrawable cd = new ColorDrawable(Color.TRANSPARENT);
// ColorDrawable dw = new ColorDrawable(-00000);// 半透明
        setBackgroundDrawable(cd);// 设置背景图片,不能在布局中设置,要通过代码来设置
        setOutsideTouchable(true);// 触摸popupwindow外部,popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功,如上

        mListView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (mItemSelectListener != null) {
            mItemSelectListener.onItemClick(i);
        }
        dismiss();
    }

    //设置适配器
    public void setAdapter(MyAdapter myAdapter) {
        this.myAdapter = myAdapter;
        this.mListView.setAdapter(this.myAdapter);
    }

    //更新列表
    public void setNotify() {
        this.myAdapter.notifyDataSetChanged();
    }

    //设置监听
    public void setItemSelectListener(MItemSelectListener mItemSelectListener) {
        this.mItemSelectListener = mItemSelectListener;
    }

    //自定义list中item的点击监听
    public interface MItemSelectListener {
        void onItemClick(int position);
    }
}
其中MyAdapter的代码如下:

public class MyAdapter extends SpinnerAdapter {

public MyAdapter(Context context) {
    super(context);
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    Holder holder;
    if (view == null) {
        view = mInflater.inflate(android.R.layout.simple_list_item_1, null);
        holder = new Holder();
        holder.textView = (TextView) view.findViewById(android.R.id.text1);
        view.setTag(holder);
    } else {
        holder = (Holder) view.getTag();
    }

    County county = (County) getItem(position);
    holder.textView.setText(county.getCountyName());
    return view;
}

private class Holder {

    public TextView textView;
}

}


其中的SpinnerAdapter可以在上一篇博客”定义属于自己的适配器“中看到

下拉列表的具体使用方法

spinnerPopWindow = new SpinnerPopWindow(this);
        myAdapter = new MyAdapter(this);
        spinnerPopWindow.setAdapter(myAdapter);
        myAdapter.refreshData(要填入的数据);
        spinnerPopWindow.setWidth(下拉列表的宽度);
        spinnerPopWindow.setItemSelectListener(new SpinnerPopWindow.MItemSelectListener() {
                    @Override
                    public void onItemClick(int position) {

                    }
                });
                //列表框要显示的位置,表示显示在控件view的下方,显示下拉列表
                spinnerPopWindow.showAsDropDown(view);

你可能感兴趣的:(android,自定义,下拉列表,PopupWindo)