Android点击穿透PopupWindow [英] Android Click Through PopupWindow

查看:1183
本文介绍了Android点击穿透PopupWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个扩展popupwindow的类.其构造函数如下所示:

I have created a class that extends popupwindow. its constructor looks something like the following

super(builder.context.get());

this.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT);
setFocusable(true);
setBackgroundDrawable(new BitmapDrawable());
setTouchInterceptor(onTouchListener);
FrameLayout frameLayout = new FrameLayout(builder.context.get());
LayoutInflater inflater = (LayoutInflater) builder.context.get().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);
View overlayBase = inflater.inflate(R.layout.tutorial_view_items, null, false);
frameLayout.addView(builder.backgroundView);
frameLayout.addView(overlayBase);
setContentView(frameLayout);

onTouchListener如下所示:

the onTouchListener looks like the following:

private OnTouchListener onTouchListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        Log.d(TAG, "Received");

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(radius, 2)) {
            Log.d(TAG, "bubbled through");
            return false;
        }
        return true;
    }
};

要实际显示弹出窗口,我呼叫SomePopupWindow.showAtLocation(SomeActivity.getWindow().getDecorView().getRootView(), Gravity.CENTER, 0, 0);

to actually display the popupwindow, I call SomePopupWindow.showAtLocation(SomeActivity.getWindow().getDecorView().getRootView(), Gravity.CENTER, 0, 0);

形象地表示,这是有问题的产品

Visually represented, here is the product in question

touchListener正在响应,但是输入没有发送到基础活动或popupwindow? (该按钮没有响应,就好像我要删除ontouchlistener一样,该按钮也能正常工作.)

The touchListener is responding, but the input does not get sent to the underlying activity OR the popupwindow? (the button is unresponsive, where as if i were to remove the ontouchlistener, the button works fine).

更新 将触摸侦听器设置为frameLayout(在这种情况下,即popupwindow的内容视图)而不是popupwindow本身,可以让我单击弹出窗口中定义的按钮.仍在寻找将触摸事件委托给基础活动/视图的方法

update setting the touch listener to the frameLayout (i.e. content view of popupwindow in this case) instead of the popupwindow itself allows me to click on the buttons defined in the popup. still looking for way to delegate the touch event to the underlying activity/view

解决方案

为Popupwindow注册一个触摸拦截器.如果您想让活动来处理它,请假定您对该活动具有引用,请调用someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);;如果您希望弹出窗口甚至可以处理触摸,请在弹出窗口的内容视图中调用someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor). ,如在popupwindows setContentView(someView)方法中设置的.

Register a touch interceptor for the Popupwindow. If you want the activity to handle it, assuming you have a reference to the activity, call someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);, if you want the popupwindow to handle the touch even, call someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor) to the popupwindow's content view, as set in the popupwindows setContentView(someView) method.

我的方法具体如下所示

private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
                radius, 2)) {
            activity.get().dispatchTouchEvent(event);
            return false;
        }
        frameLayout.dispatchTouchEvent(event);
        return true;
    }
};

推荐答案

解决方案

为Popupwindow注册一个触摸拦截器.如果您想让活动来处理它,请假定您对该活动具有引用,请调用someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);;如果您希望弹出窗口甚至可以处理触摸,请在弹出窗口的内容视图中调用someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor). ,如在popupwindows setContentView(someView)方法中设置的.

Register a touch interceptor for the Popupwindow. If you want the activity to handle it, assuming you have a reference to the activity, call someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);, if you want the popupwindow to handle the touch even, call someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor) to the popupwindow's content view, as set in the popupwindows setContentView(someView) method.

我的方法具体如下所示

private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
                radius, 2)) {
            activity.get().dispatchTouchEvent(event);
            return false;
        }
        frameLayout.dispatchTouchEvent(event);
        return true;
    }
};

这篇关于Android点击穿透PopupWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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