Android deja de enviar eventos onTouch
Tengo una vista de encargo que deseo manejar acontecimientos del tacto para. En particular, arrastrando un dedo sobre la vista debería hacer que su contenido se desplazara. Para ello, he implementado el siguiente onTouchListener:
private OnTouchListener graphOnTouchListener = new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { System.out.println("onTouch called"); System.out.println("view width: " + v.getWidth()); System.out.println("view height: " + v.getHeight()); System.out.println("view: " + v); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: // First finger down System.out.println("ACTION_DOWN x: " + event.getX() + ", y: " + event.getY()); startTouch(event.getX()); break; case MotionEvent.ACTION_UP: // Last finger to be removed System.out.println("ACTION_UP x: " + event.getX() + ", y: " + event.getY()); break; case MotionEvent.ACTION_MOVE: // A finger moves System.out.println("ACTION_MOVE x: " + event.getX() + ", y: " + event.getY()); moveTouch(event.getX()); break; default: break; } // The event has been handled, so return true return true; } };
Esto funciona como se espera, a menos que el usuario mueve su dedo hacia arriba o hacia abajo mientras está en la vista. En este punto, los eventos de toque dejan de ser enviados (como se evidencia por la falta de salida en LogCat). Sin embargo, el uso de adb shell getevent
todavía muestra los eventos que se generan por el movimiento táctil.
- OnTouchevent () vs onTouch ()
- "Presione y mantenga presionado" en Android necesita cambiar estados (selector XML personalizado) usando onTouchListener
- Cambiar la altura del diseño relativo con la misma velocidad que el dedo del usuario Se mueve en el diseño
- Cómo implementar tanto ontouch y también onfling en un mismo listview?
- Analog SetOnTouchListener o cómo se utiliza en xamarin, qué argumento debe ser
¿Puede alguien sugerir cómo puedo rectificar esto?
Según lo solicitado, el XML de diseño se ve así:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#ffffff"> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dip"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <com.graphlib.ShaderAreaGraphView android:layout_width="fill_parent" android:layout_height="200dip" android:id="@+id/activity_power_realtime" android:background="#000000" /> <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:gravity="center"> <Button android:text="<<" android:id="@+id/goBackwardsButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="Zoom out" android:id="@+id/zoomOutButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="Zoom in" android:id="@+id/zoomInButton" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button> <Button android:text=">>" android:id="@+id/goForwardsButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> </LinearLayout> </ScrollView> </FrameLayout> </LinearLayout>
- Android: ImageView getID (); Devolver entero
- Problema en OnTouchListener para ImageView
- Obtener coordenadas reales de xy de los usuarios
- GC y onTouch causan un error de señal fatal 11 (SIGSEGV) en la aplicación que utiliza ffmpeg a través de ndk
- EditText en ListView pierde foco cuando se presiona en Android 4.x
- Android: ¿Qué uso tiene AnchorView de MediaController?
- Diseño con posición dinámica
- ¿Cómo manejar eventos de toque en un fragmento?
necesita escribir la instrucción break en cada caso. de lo contrario todos los casos se ejecutan.
case MotionEvent.ACTION_DOWN: // First finger down System.out.println("ACTION_DOWN x: " + event.getX() + ", y: " + event.getY()); startTouch(event.getX()); break; case MotionEvent.ACTION_UP: // Last finger to be removed System.out.println("ACTION_UP x: " + event.getX() + ", y: " + event.getY()); break; case MotionEvent.ACTION_MOVE: // A finger moves System.out.println("ACTION_MOVE x: " + event.getX() + ", y: " + event.getY()); moveTouch(event.getX()); break;
o puede poner en el if … else si la condición
Resulta que el problema es que la vista está contenida en un ScrollView. La eliminación de ScrollView corrige el problema.
Una manera fácil de resolver esto es crear una referencia a la vista de desplazamiento, por ejemplo, myScrollView = (ScrollView) findViewById (R.id.myScrollView). A continuación, en estado onTouchEvent, ACTION_DOWN:
myScrollView.requestDisallowInterceptTouchEvent (true);
Y en ACTION_UP
myScrollView.requestDisallowInterceptTouchEvent (false);
En la parte inferior del conmutador, intenta agregar
default: break;
Puede que no sea la solución, pero es una buena práctica tener eso.