Clique no Android através do PopupWindow
Eu criei uma classe que estende a janela pop-up. seu construtor se parece com o seguinte
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);
o onTouchListener se parece com o seguinte:
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;
}
};
para realmente exibir a janela pop-up, eu chamoSomePopupWindow.showAtLocation(SomeActivity.getWindow().getDecorView().getRootView(), Gravity.CENTER, 0, 0);
Representado visualmente, aqui está o produto em questão
O touchListener está respondendo, mas a entrada não é enviada para a atividade subjacente OU para a janela pop-up? (o botão não responde, onde, como se eu fosse remover o ontouchlistener, o botão funciona bem).
atualizar definir o ouvinte de toque como frameLayout (ou seja, exibição de conteúdo da janela pop-up neste caso) em vez da própria janela pop-up, permite-me clicar nos botões definidos na janela pop-up. ainda está procurando uma maneira de delegar o evento de toque na atividade / exibição subjacente
SOLUÇÃO
Registre um interceptor de toque na janela Popup. Se você deseja que a atividade lide com ela, supondo que você tenha uma referência à atividade, chamesomeActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);
, se você quiser que a janela pop-up lide com o toque uniformemente, ligue parasomeView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor)
para a janela pop-upvisualização de conteúdo, conforme definido nas janelas pop-upsetContentView(someView)
método.
Meu método se parecia especificamente com o seguinte
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;
}
};