Haga clic en Android a través de PopupWindow

He creado una clase que extiende popupwindow. Su constructor se parece a lo siguiente

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

El onTouchListener se parece a lo siguiente:

 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 mostrar realmente el popupwindow, llamo SomePopupWindow.showAtLocation(SomeActivity.getWindow().getDecorView().getRootView(), Gravity.CENTER, 0, 0);

Representado visualmente, aquí está el producto en cuestión

paja

El touchListener está respondiendo, pero la entrada no se envía a la actividad subyacente o la ventana popup? (El botón no responde, donde como si fuera a quitar el ontouchlistener, el botón funciona bien).

Actualizar la configuración de la escucha de tacto a la frameLayout (es decir, la vista de contenido de popupwindow en este caso) en lugar de la popupwindow en sí me permite hacer clic en los botones definidos en la ventana emergente. Todavía está buscando la manera de delegar el evento táctil a la actividad / vista subyacente

SOLUCIÓN

Registrar un interceptor táctil para el Popupwindow. Si desea que la actividad lo maneje, suponiendo que tenga una referencia a la actividad, llame a someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter); , Si desea que el popupwindow maneje el toque incluso, llame a someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor) a la vista de contenido del popupwindow, como se establece en el método setContentView(someView) .

Mi método se parecía específicamente a los siguientes

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

SOLUCIÓN

Registrar un interceptor táctil para el Popupwindow. Si desea que la actividad lo maneje, suponiendo que tenga una referencia a la actividad, llame a someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter); , Si desea que el popupwindow maneje el toque incluso, llame a someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor) a la vista de contenido del popupwindow, como se establece en el método setContentView(someView) .

Mi método se parecía específicamente a los siguientes

 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; } }; 
 public class TestPopupActivity extends Activity { //The "x" and "y" position of the "Show Button" on screen. Point p; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn_show = (Button) findViewById(R.id.show_popup); btn_show.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //Open popup window if (p != null) showPopup(TestPopupActivity.this, p); } }); } // Get the x and y position after the button is draw on screen // (It's important to note that we can't get the position in the onCreate(), // because at that stage most probably the view isn't drawn yet, so it will return (0, 0)) @Override public void onWindowFocusChanged(boolean hasFocus) { int[] location = new int[2]; Button button = (Button) findViewById(R.id.show_popup); // Get the x, y location and store it in the location[] array // location[0] = x, location[1] = y. button.getLocationOnScreen(location); //Initialize the Point with x, and y positions p = new Point(); px = location[0]; py = location[1]; } // The method that displays the popup. private void showPopup(final Activity context, Point p) { int popupWidth = 200; int popupHeight = 150; // Inflate the popup_layout.xml LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup); LayoutInflater layoutInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup); // Creating the PopupWindow final PopupWindow popup = new PopupWindow(context); popup.setContentView(layout); popup.setWidth(popupWidth); popup.setHeight(popupHeight); popup.setFocusable(true); // Some offset to align the popup a bit to the right, and a bit down, relative to button's position. int OFFSET_X = 30; int OFFSET_Y = 30; // Clear the default translucent background popup.setBackgroundDrawable(new BitmapDrawable()); // Displaying the popup at the specified location, + offsets. popup.showAtLocation(layout, Gravity.NO_GRAVITY, px + OFFSET_X, py + OFFSET_Y); // Getting a reference to Close button, and close the popup when clicked. Button close = (Button) layout.findViewById(R.id.close); close.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { popup.dismiss(); } }); } } 

Prueba este código puede ser que te ayude. … . O puede ser este enlace le ayudará u más .. http://mrbool.com/how-to-implement-popup-window-in-android/28285

  • Onclicklistener no está trabajando en recyclerview
  • ¿Cómo puedo activar el evento OnClick mediante programación en android?
  • Cómo deshabilitar OnClickListerners
  • SetOnclickListener vs OnClickListener vs View.OnClickListener
  • Creación de OnClickListener personalizado
  • Android - mismo OnClickListener para TextView y ImageView
  • RecyclerView onClick
  • onClickListener a un botón del teclado
  • Añada dinámicamente funcionalidad a la función onclicklistener de Button en Android
  • GetOnClickListener () en las vistas de Android
  • OnClickListener Mantener pulsado el botón?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.