Question

I am trying to make a simple 2D game using Opengl es 2. I am using GLSurfaceView. I am trying to detect fling gesture on the view but it does not detect anything. Why is this? Following is the code :-

Tried to declare the gesture detecter inside and outside the glsurfaceview.

@SuppressWarnings("deprecation")
    final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
        public void onLongPress(MotionEvent e) {
            Log.d("", "Longpress detected");
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2,
                float velocityX, float velocityY) {
            Log.d("", "OnFling detected");
            return super.onFling(e1, e2, velocityX, velocityY);
        }


    });

Inside the GLSurfaceView

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

When i fling on the surface, it detects the long press and prints the debug message for long press rather than fling. Whatever i do on screen, it just detects longpress.. what is wrong?

Was it helpful?

Solution

Include this override for onDown in your listener:

@Override
public boolean onDown(MotionEvent event) { 
    return true;
}

From the documentation:

This is because all gestures begin with an onDown() message. If you return false from onDown(), as GestureDetector.SimpleOnGestureListener does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods of GestureDetector.OnGestureListener never get called. This has the potential to cause unexpected problems in your app."

I appreciate that this does not explain why you are still managing to get a longpress detected. If the above resolves your issue, perhaps that is a puzzle that doesn't need solving...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top