我创建了一个扩展弹出窗口的类。它的构造函数看起来像下面这样
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 如下所示:
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);
从视觉上看,这是有问题的产品
touchListener 正在响应,但输入未发送到底层活动或弹出窗口? (该按钮没有响应,就好像我要删除 ontouchlistener 一样,该按钮工作正常)。
update将触摸侦听器设置为frameLayout(即本例中popupwindow的内容视图)而不是popupwindow本身允许我单击弹出窗口中定义的按钮。仍在寻找将触摸事件委托给底层活动/视图的方法
SOLUTION
为 Popupwindow 注册一个触摸拦截器。如果您希望活动处理它,假设您有对该活动的引用,请调用someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);
,如果您希望弹出窗口能够处理触摸,请调用someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor)
到弹出窗口的内容视图,如弹出窗口中设置的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;
}
};