2011-02-08 20 views
9

Quindi, mi piacerebbe essere in grado di riconoscere i gesti avanzati utilizzando GestureOverlayView e OnGestureListener così come essere in grado di rilevare i doppi tocchi e le lunghe pressioni con GestureDetector. Ho entrambe le funzioni che funzionano bene separatamente in due diversi progetti. Tuttavia, sto provando a combinarli e sto incontrando alcuni problemi. Sembra che quando tento una pressione doppia/lunga, non viene riconosciuto perché GestureOverlayView copre l'intero schermo e riconosce solo i gesti avanzati definiti nel Gesture Builder. Qualcuno sa come configurare GestureOverlayView in modo che consenta a GestureDetector di eseguire il proprio lavoro? Il mio codice:GestureOverlayView e Double-Tap

public class HelloAndroid extends Activity implements OnGesturePerformedListener, OnDoubleTapListener, OnGestureListener { 
/** Called when the activity is first created. */ 
private GestureLibrary mLibrary; 
private GestureDetector detector; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures); 
    if (!mLibrary.load()) { 
     finish(); 
    } 

    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures); 
    gestures.addOnGesturePerformedListener(this); 

    detector = new GestureDetector(this, this);\ 

E il xml ...

<?xml version="1.0" encoding="utf-8"?> 

<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/gestures" 
     android:layout_width="fill_parent" 
     android:visibility="invisible" 
     android:layout_height="fill_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 
    <TextView 
     android:id="@+id/text1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" 
     /> 
    </LinearLayout>  
</android.gesture.GestureOverlayView> 

Grazie in anticipo!

risposta

3

L'ho fatto, ma non sono sicuro se la mia soluzione funzionerà per voi. È necessario rilevare il doppio tocco e la pressione prolungata sull'intera attività o solo nell'elenco?

Nel mio progetto, quello che ho è, ovviamente, GestureOverlayView che avvolge tutto, ma il mio GestureDetector è stato assegnato solo sul contenitore che voglio gestire quegli eventi, in particolare su una griglia con le miniature.

In realtà, il mio gesto è rivelatore all'interno in un View.OnTouchListener

public class TouchListener implements View.OnTouchListener { 

    Rect rect = new Rect(); 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     gestureDetector.onTouchEvent(event); 
     // We do not use the return value of 
     // mGestureDetector.onTouchEvent because we will not receive 
     // the "up" event if we return false for the "down" event. 
    // false is returned so children views can receive touch event too. 
     return false; 
} 

    private View getViewAt(int x, int y) { 
    // I need to know which thumbnail was selected 
    } 

    GestureDetector gestureDetector = new GestureDetector(new SimpleOnGestureListener() { 
     @Override 
     public void onLongPress(MotionEvent e) {…} 
     @Override 
     public boolean onSingleTapConfirmed(MotionEvent e) {…} 
     @Override 
     public boolean onDoubleTap(MotionEvent e) {...} 

}); 

}

Poi ho solo bisogno di assegnare questo tocco ascoltatore alla mia griglia. Nel tuo caso potrebbe essere usato nella tua lista.

mGrid.setOnTouchListener(new TouchListener()); 

Succede che GesturOverlayView propagherà eventi touch alle viste interne quando non viene rilevato un gesto. La soluzione è creare un touchlistener in vista che è necessario rilevare semplici gesti utilizzando GestureDetector sul metodo onTouch del listener. Spero che possa aiutarti.

+0

Questo ha funzionato. Grazie ! –

0

Il problema deriva dal metodo onTouchEvent (evento) della propria attività che non viene chiamato perché l'evento è intercettato da GestureOverlayView.

Se si desidera ottenere questo evento nella propria attività, è possibile sovrascrivere il metodo dispatchTouchEvent (evento).

Nel vostro caso, questo codice può funzionare:

@Override 
public boolean dispatchTouchEvent(MotionEvent e) 
{ 
    detector.onTouchEvent(e); 

    return super.dispatchTouchEvent(e); 
} 
0

ho avuto lo stesso problema.

GestureOverlayView sta intercettando onTouchEvent. Quindi, solo set

android:eventsInterceptionEnabled="false" 

Il risultato dovrebbe essere qualcosa di simile:

<android.gesture.GestureOverlayView 
     android:id="@+id/gestures" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:eventsInterceptionEnabled="false" > 
0

stavo avendo il qualche problema. Ho trovato una soluzione ma non so perché funzioni.

Uso il metodo dispathTouchEvent ma chiamo suTouchEvent() con la mia istanza GestureDetector.

@Override 
public boolean dispatchTouchEvent(MotionEvent ev) 
{ 
    this.myGestureDetector.onTouchEvent(ev); 
    return super.dispatchTouchEvent(ev); 
} 
Problemi correlati