Android PopupWindow的使用

项目用到的,实现了需要的功能,备用

 // 屏幕的width
    private int mScreenWidth;
    // 屏幕的height
    private int mScreenHeight;
    //创建一个popwindows
    private PopupWindow mPopupWindow;
//弹出pop的控件监听
    @OnClick(R.id.service_firm)
    public void service_firm(){
        getPopupWindowInstance();
        mPopupWindow.showAsDropDown(mService_firm);
    }
    /*
     * 获取PopupWindow实例
     */
    private void getPopupWindowInstance() {
        if (null != mPopupWindow) {
            mPopupWindow.dismiss();
            return;
        } else {
            initPopuptWindow();
        }
    }
    /*
     * 创建PopupWindow
     */
    private void initPopuptWindow() {
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        View view = layoutInflater.inflate(R.layout.pop_gongsi, null);
        ListView mLv_gongsi = (ListView) view.findViewById(R.id.lv_list);
        // 获取屏幕和PopupWindow的width和height
        /*mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();
        mScreenHeight = getWindowManager().getDefaultDisplay().getHeight();*/
        FuWuGongSiAdapter adapter_fuwugongsi = new FuWuGongSiAdapter(this);
        mLv_gongsi.setAdapter(adapter_fuwugongsi);
        adapter_fuwugongsi.setData(list_name);
        adapter_fuwugongsi.notifyDataSetChanged();
        mLv_gongsi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GongSiBean gongsi = list_name.get(0);
                mService_firm.setText(gongsi.getCompanyInfoShortname());
                mPopupWindow.dismiss();
            }
        });
<pre name="code" class="java">        // 参数1:contentView 指定PopupWindow的内容
        // 参数2:width 指定PopupWindow的width
        // 参数3:height 指定PopupWindow的height
        mPopupWindow = new PopupWindow(view,mService_firm.getWidth() , LinearLayout.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("mengdd", "onTouch : ");
                return false;
                // 这里如果返回true的话,touch事件将被拦截,拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
            }
        });
        // 设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.update();
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());

    }
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值