Android: delegate touch event to underlaying view

前端 未结 3 776
旧时难觅i
旧时难觅i 2020-12-24 13:54

I have the following hierarchy: Activity -> PopupWindow -> CustomView

My the PopupWindow itself is a square, but

相关标签:
3条回答
  • 2020-12-24 14:05

    It's too bad that the PopupWindow is not a subclass of ViewGroup or even View. Then you could override dispatchTouchEvent. Can you modify your custom view to know about the PopupWindow and call dismiss() on it when there is a click outside the circle?

    0 讨论(0)
  • 2020-12-24 14:14

    Try setting these on your PopupWindow instance:

    window.setBackgroundDrawable(new BitmapDrawable());

    window.setOutsideTouchable(true);

    and

    window.setTouchable(true);

    This is just a suggestion... I haven't tested it. Let me know if it works.

    [Note: This is for the onTouch() to get called on touch events..]

    0 讨论(0)
  • 2020-12-24 14:24

    Consider the FLAG_NOT_TOUCH_MODAL:

    /** Window flag: Even when this window is focusable (its
      * {@link #FLAG_NOT_FOCUSABLE is not set), allow any pointer events
      * outside of the window to be sent to the windows behind it.  Otherwise
      * it will consume all pointer events itself, regardless of whether they
      * are inside of the window. */
    public static final int FLAG_NOT_TOUCH_MODAL    = 0x00000020;
    

    You can use this code to upate the window flags after the popup window was shown.

    FrameLayout popupContainer = (FrameLayout)contentView.getParent();
    WindowManager.LayoutParams p = (WindowManager.LayoutParams)popupContainer.getLayoutParams();
    p.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
    WindowManager windowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
    windowManager.updateViewLayout(popupContainer, p);
    
    0 讨论(0)
提交回复
热议问题