PopupWindow TouchInterceptor not working

僤鯓⒐⒋嵵緔 提交于 2019-12-01 05:01:12

问题


I'm trying to test the PopupWindow class. I've created this method to show the popup:

    public void showPopup(){
            LayoutInflater layoutInflater   = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
            View popupView = layoutInflater.inflate(R.layout.popup, null);
            final PopupWindow popup = new PopupWindow(popupView, 
                       LayoutParams.WRAP_CONTENT,  
                         LayoutParams.WRAP_CONTENT);  
            popup.setOutsideTouchable(true);
            popup.setTouchable(true);
            popup.setTouchInterceptor(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Log.d("POPUP", event.toString());
                    if(event.getAction() == MotionEvent.ACTION_OUTSIDE){
                        popup.dismiss();
                        return true;
                    }
                    return true;
                }
            });
            popup.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 200);
}

The popup is showing correctly,by the way it seems that the Touch Interceptor don't work at all: I don't get any log information and of course the popup is not dismissing if pressing outside of it.

Is there some further property I have to set in the popup or in the Activity which host it?


回答1:


 pw.setBackgroundDrawable (new BitmapDrawable());
 pw.setFocusable(false);
 pw.setOutsideTouchable(true); 

use this code hope this is helpful




回答2:


If you want to do some action, when it's clicked outside window and both setFocusable() + setOutsideTouchable() you need true, you might consider using setOnDismissListener. It's method onDismiss is called, as expected, when dialog dialog is dismissed:

  PopupWindow mPopupWindow = new PopupWindow(mRootView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
  mPopupWindow.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));
  mPopupWindow.setFocusable(true);
  mPopupWindow.setOutsideTouchable(true);
  mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
      @Override
      public void onDismiss() {
        // some action ....
      }
  });


来源:https://stackoverflow.com/questions/15570172/popupwindow-touchinterceptor-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!