点击某个View弹出popupwindow列表:
代码:
private ArrayAdapter<String> adapter_huoMing;
private PopupWindow popupWindow;
    /**
     * 这里是popupwindow,用来显示所有查询出来的信息列表
     * 这里的View是调用的时候传入界面中的一个View,popupwindow则显示在此view的下方
     */
    private void showPopupWindow(View view) {
        // 一个自定义的布局,作为显示的内容
        View contentView = LayoutInflater.from(getApplicationContext())
                .inflate(R.layout.activity_popuwindow_huoming, null);
        // 设置按钮的点击事件
        ListView lv_popup = (ListView) contentView
                .findViewById(R.id.lv_popup_huoming);
        //这里R.layout.activity_changnzytd_item为列表中每一项的布局,R.id.tv_changnzytd
          为显示数据的textview,huoMing为列表数据源
        adapter_huoMing = new ArrayAdapter<String>(getApplicationContext(),
                R.layout.activity_changnzytd_item, R.id.tv_changnzytd, huoMing);
        lv_popup.setAdapter(adapter_huoMing);
        lv_popup.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // 这里写Item被点击后的事件
                // Toast.makeText(getApplicationContext(), "1", 0).show();
                if (huoMing.length > 0) {
                    et_huoming.setText(huoMing[position]);
                    popupWindow.dismiss();
                }
            }
        });
        popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("mengdd", "onTouch : ");
                return false;
                // 这里如果返回true的话,touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
            }
        });
        // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.abc_btn_borderless_material));
        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);
    }activity_popuwindow_huoming://这个是要显示在popupwindow中的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:orientation="vertical" > <ListView android:id="@+id/lv_popup_huoming" android:layout_width="fill_parent" android:layout_height="250dp" > </ListView> </LinearLayout>
本文出自 “移动平台开发” 博客,请务必保留此出处http://liuxudong1001.blog.51cto.com/10877072/1748649
利用popupwindow生成带有列表的对话框,并设置对话框列表的点击事件
原文:http://liuxudong1001.blog.51cto.com/10877072/1748649