PopupWindow中添加listview

SpinnerPopupWindow

public class SpinnerPopupWindow extends PopupWindow {
     
    private Context context;
    private ListView listView;
    private SpinnerAdapter adapter;
    private IOnItemClickListener listener;
    private LinearLayout transparent;
    private TextView textView;


    public SpinnerPopupWindow(Context context){
     
        this.context=context;
        init();
    }

    public void setAdapter(SpinnerAdapter adapter){
     
        this.adapter=adapter;
        listView.setAdapter(adapter);
    }

    //设置监听器
    public void setItemListener(IOnItemClickListener listener){
     
        this.listener=listener;
    }


    public void init(){
     
        View view= LayoutInflater.from(context).inflate(R.layout.spiner_window_layout,null);
        setContentView(view);

        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

        //setFocusable(true);//获取焦点
//        setOutsideTouchable(true);//外部是否能点击(点击外部是否让popupwindow消失)
        listView= (ListView) view.findViewById(R.id.listview);
        listView.setOnItemClickListener(onItemClickListener);
        transparent=(LinearLayout) view.findViewById(R.id.transparent);
        transparent.setOnClickListener(onClickListener);

    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
     
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
     
            dismiss();
            if(listener!=null) {
     
                listener.onItemClick(position);
                dismiss();
            }
        }
    };

    private View.OnClickListener onClickListener = new View.OnClickListener() {
     
        @Override
        public void onClick(View view) {
     
            dismiss();
        }
    };


    public interface IOnItemClickListener {
     
        void onItemClick(int position);
    }
}

spiner_window_layout布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="#fff"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:dividerHeight="1dp"
        android:scrollbars="none"
        android:background="#fff"></ListView>

    <LinearLayout
        android:id="@+id/transparent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="#00000000"></LinearLayout>

</LinearLayout>

SpinnerAdapter

public class SpinnerAdapter extends BaseAdapter {
     
    private Context context;
    private List<String> list;
    private int pos=0;

    public SpinnerAdapter(Context context, List<String> list) {
     
        this.context=context;
        this.list=list;
    }

    public void exchange(int pos){
     
        this.pos=pos;
    }

    @Override
    public int getCount() {
     
        return list.size();
    }

    @Override
    public Object getItem(int position) {
     
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
     
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
     
        ViewHolder viewHolder;
        if(convertView==null){
     
            convertView= LayoutInflater.from(context).inflate(R.layout.item_name,null);
            viewHolder=new ViewHolder();
            viewHolder.tv= (TextView) convertView.findViewById(R.id.tv);
            convertView.setTag(viewHolder);
        }else{
     
            viewHolder=(ViewHolder)convertView.getTag();
        }
        viewHolder.tv.setText(list.get(position));

        return convertView;
    }


    private class ViewHolder{
     
        TextView tv;
    }
}

item_name布局




    



使用

		//初始化
        SpinnerPopupWindow spinnerPopupWindow = new SpinnerPopupWindow(EmergencyCommandActivity.this);
        SpinnerAdapter spinnerAdapter = new SpinnerAdapter(EmergencyCommandActivity.this, name_list);
        spinnerPopupWindow.setAdapter(spinnerAdapter);
        spinnerPopupWindow.setItemListener(onItemClickListener);
        spinnerPopupWindow.setWidth();//设置宽度
        spinnerPopupWindow.setHeight();//设置高度
        
        //显示PopupWindow
         spinnerPopupWindow.showAsDropDown();//显示在某个控件下方
		//判断PopupWindow是否是显示状态
		spinnerPopupWindow.isShowing()

你可能感兴趣的:(安卓,android)