Android:将触摸事件委托给底层视图 [英] Android: delegate touch event to underlaying view

查看:217
本文介绍了Android:将触摸事件委托给底层视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下层次结构:活动 - > PopupWindow - > CustomView

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

我的 PopupWindow 本身是一个方形,但是透明,所以你可以看到活动坐在后台 CustomView 是嵌入PopupWindow内的一个圆圈。

My the PopupWindow itself is a square, but transparent, so you can see the Activity sitting in the background. The CustomView is a circle embedded inside the PopupWindow.

我有什么目前为止已实现的是

What I have achieved so far is


  1. 用户点击绿色圆圈,我调用某些东西

  2. 用户点击 PopupWindow 之外的点击,触摸事件将被发送到活动。

  1. User clicks on green circle and I invoke "some stuff"
  2. User clicks outside of the PopupWindow and the touch event gets dispatched to the Activity.

现在缺少的部分是发送在 PopupWindow 内发生的任何触摸事件,但在 CustomView (圈子)之外到活动。

The missing part is now, to dispatch any touch event that happens inside the PopupWindow but outside the CustomView (circle) to the Activity.

我已经知道触摸在我的圈子之外的感觉。我只是在将其委托给活动时遇到问题。

I already know how to sense when the touch is outside of my circle. I just have problems delegating it to the Activity.

在我的 CustomView 中,我在 onTouch

if (radiusTouch > maxRadius) {
    return false;
}

在我的 PopupWindow 我已经设置了以下内容,但是它从未被调用过:

In my PopupWindow I already setup the following, but it gets never called:

popup.setTouchInterceptor(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.i(TAG, "PopupWindow :: onTouch()");
        return false;
    }
});

还有其他任何事情可以将触摸事件委托给活动?

Anything else I have to do to delegate the touch event all the way to the Activity?

推荐答案

考虑FLAG_NOT_TOUCH_MODAL:

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);

这篇关于Android:将触摸事件委托给底层视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆