Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GestureOverlayView and Double-Tap

Tags:

android

So, I would like to be able to recognize advanced Gestures using the GestureOverlayView and OnGestureListener as well as be able to detect double-taps and long presses using GestureDetector. I have both features working fine separately in two different projects. However, I am trying to combine them and am running into some problems. It seems that when I attempt a double-tap / long press, it is not being recognized because the GestureOverlayView is covering the whole screen and will only recognize the advanced gestures defined in the Gesture Builder. Does anyone know how to set-up the GestureOverlayView so that it will allow GestureDetector to do it's job? My code:

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

And the 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>

Thanks in advance!

like image 914
Basel Qumsiyeh Avatar asked Feb 08 '11 07:02

Basel Qumsiyeh


2 Answers

I did it, but not sure if my solution will work for you. Do you need to detect double tap and longpress on the whole activity or just in your list?

In my project, what I have is, of course GestureOverlayView wrapping everything, but my GestureDetector was assigned only on the container I want to handle those event, specifically on a Grid with thumbnails.

Actually, my gesture detector is inside in a 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) {...}

});

}

Then I just need to assign this touch listener to my grid. In your case it may be used on your list.

mGrid.setOnTouchListener(new TouchListener());

What happens is that GesturOverlayView will propagate touch events to inner views while a gesture is not detected. The solution is create a touchlistener on view you need to detect simple gestures using GestureDetector on listener onTouch method. Hope it may help you.

like image 186
fr4gus Avatar answered Oct 22 '22 17:10

fr4gus


The issue comes from the method onTouchEvent(event) of your activity that is not called because the event is intercepted by the GestureOverlayView.

If you want to get this event in your activity you might want to override the dispatchTouchEvent(event) method.

In your case, this code may work :

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

    return super.dispatchTouchEvent(e);
}
like image 1
Kéza Avatar answered Oct 22 '22 15:10

Kéza