2010-11-22 12 views
6

Tengo una Actividad que implementa un Detector de gestos para capturar la entrada de fling del usuario para la navegación a otras pantallas. Eso estaba funcionando bien, pero recientemente actualicé una clase que se deriva de BaseActivity para agregar una función onClick y ahora ese evento de clic parece bloquear el onFling de ser golpeado. El onClick está vinculado a un área de TextView (en LinearLayout) que tengo en mi pantalla. El método resultsClick está conectado a TextView utilizando su propiedad onClick en el diseño XML.Android onClick bloqueando onFling

He intentado cambiar los valores de retorno en onSingleTapUp y onDown sin suerte. También intenté agregar instrucciones de registro a todas las funciones a continuación. Ninguno de ellos dispara cuando lanzo en el área de TextView pero lo hacen en otras áreas de la pantalla.

Tal vez estoy usando términos de búsqueda erróneos, pero parece que no puedo encontrar un ejemplo que aborde esto; sin embargo, estoy seguro de que este problema se ha resuelto antes.

public class DerivedActivity extends BaseActivity 
{ 
    ... 
    /** 
    * resultsClick - The user clicked on the Results area 
    * @param v 
    */ 
    public void resultsClick(View v) 
    { 
     try 
     { 
     Log.i(this.toString(), "resultsClick"); 
     startActivity(new Intent(this, Results_TabHost.class)); 
     } 
     catch (Exception e) 
     { 
     Log.e(this.toString(), "Exception" + e.toString()); 
     } 

    }// end resultsClick 
    ... 
} 

Aquí es la clase base que implementa el código GestureListener

public class BaseActivity extends ActivityGroup 
          implements OnGestureListener 
{ 
    ... 
    private static final int SWIPE_MIN_DISTANCE = 120; 
    private static final int SWIPE_MAX_OFF_PATH = 250; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 200; 

    public boolean onFling(MotionEvent e1, 
          MotionEvent e2, 
          float velocityX, 
          float velocityY) 
    { 
     try 
     { 
     Log.i(this.toString(), "onFling"); 

     // jump right out if not a swipe/fling 
     if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
     { 
      return false; 
     } 

     // right to left swipe 
     if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && 
      Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
     { 
      Log.i(this.toString(), "fling left"); 
      rightArrowClick(null); 

     } 
     else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && 
        Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
     { 
      Log.i(this.toString(), "fling right"); 
      leftArrowClick(null); 
     } 
     } 
     catch (Exception e) 
     { 
     Log.e(this.toString(), "Exception" + e.toString()); 
     } 

     return true; 

    }// end onFling 

    // These next methods we are required to have - even if unused - 
    // in order for the Gesture Handling to work 

    @Override 
    public boolean onTouchEvent(MotionEvent motionEvent) 
    { 
     return this.gestureDetector.onTouchEvent(motionEvent); 
    } 

    @Override 
    public void onLongPress(MotionEvent e) 
    { 
     // Intentionally not handling - must be overridden by listener class 
    } 

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) 
    { 
     // Intentionally not handling - must be overridden by listener class 
     // Intentionally returning true - per code examples 
     return true; 
    } 

    @Override 
    public void onShowPress(MotionEvent e) 
    { 
     // Intentionally not handling - must be overridden by listener class 
    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent e) 
    { 
     // Intentionally not handling - must be overridden by listener class 
     // Intentionally returning true - per code examples 
     return true; 
    } 

    @Override 
    public boolean onDown(MotionEvent e) 
    { 
     // Intentionally not handling - must be overridden by listener class 
     // Intentionally returning true - per code examples 
     return true; 
    } 
... 
} 

Respuesta

3

Su puesta en práctica de onTouchEvent es incorrecta. Simplemente devuelve el valor del resultado del gestureDector.

Si su detector de gestos no detecta ningún gesto, le dice a la persona que llama "No tenía nada que hacer aquí" y el evento táctil nunca se enviará a los hijos de la actividad.

Debe llamar al super.onTouchEvent() si su detector de gestos no manejó el evento.

@Override 
public boolean onTouchEvent(MotionEvent motionEvent) 
{ 
    if(this.gestureDetector.onTouchEvent(motionEvent)) 
    { 
     return true; 
    } 
    //no gesture detected, let Activity handle touch event 
    return super.onTouchEvent(motionEvent); 
} 
+0

@CodeFusionMbile - gracias por su sugerencia. Intenté actualizar el evento onTouch utilizando tu código anterior, pero no influyó en el comportamiento que estoy viendo. Agregué un registro a cada uno de los eventos de gestos para asegurarme. Cuando intento deslizarme por la pantalla, veo el registro desde el clic de resultados, pero no veo que ninguno de los otros se conecte. – bursk

1

Usted sólo puede devolver false, cuando no se hace nada por su código ... Se le permitirá al sistema de eventos de movimiento manejar todo por sí mismo. Devuelve verdadero, cuando se quiere parar la prueba para ser enviados a otros niño ver ..

3

Por favor prestar atención a esta función:

@Override 
    public boolean onDown(MotionEvent e) 
    { 
     // Intentionally not handling - must be overridden by listener class 
     // Intentionally returning true - per code examples 
     return true; 
    } 

favor cambia el valor de retorno a falso.