Android非模式PopupWindow

问题描述 投票:0回答:2

请注意:此问题是关于Android PopupWindow,而不是Dialog。

我正在尝试使Android PopupWindow成为非模态:

  • 单击窗口的INSIDE时,我希望窗口的视图正常地对单击做出反应
  • [单击窗口的外侧时,我希望窗口外部的其他图形元素能够正常接收该单击(我的意思是MotionEvent),就像未显示PopupWindow一样。

我已经在PopupWindow的内容视图中设置了触摸侦听器,以测试事件是否在PopupWindow之外,如果是,则返回false。我希望然后将MotionEvent传播到单击的视图。但似乎由于这些视图不在当前窗口之内,因此它们没有收到事件。

即使事件不在PopupWindow之外,如何将事件传播到单击的视图?

public class NonModalPopupWindow extends PopupWindow {

private static final String TAG = "NonModalPopupWindow";

public NonModalPopupWindow(View contentView, int width, int height) {
    super(width, height);
    setContentView(contentView);
    contentView.setOnTouchListener(createOnTouchListener());
    setFocusable(true);
}

private View.OnTouchListener createOnTouchListener() {
    return new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            final int x = (int) event.getX();
            final int y = (int) event.getY();

            final boolean isTouchOutside = (x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight());
            if (isTouchOutside || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                Log.e(TAG, "Touched OUTSIDE");
                // How to propagate this event so that it's received by the View shown 
                // At the location where I clicked, outside of the PopupWindow? 
                return false;
            } else {
                Log.e(TAG, "Touched INSIDE");
                return true;
            }
        }
    };
}

}

android popupwindow
2个回答
0
投票

尝试一下:


0
投票

只是您不想使用活动或Theme.Dialog的DialogFragment冒险的补充

© www.soinside.com 2019 - 2024. All rights reserved.