Search code examples
androidgesture-recognitiongestures

Problems with GestureListener


I have a class declared as this:

public class TestActivity extends Activity implements OnGestureListener {

The interface im implementing is import android.view.GestureDetector.OnGestureListener;

As class var i have a GestureDetector (private GestureDetector gestureDetector;) which i init on the onCreate event: gestureDetector = new GestureDetector(this, this);

And after this, i override the interface methods:

@Override
public boolean onDown(MotionEvent e) {
    Log.w(this.getClass().toString(), e.toString());
    return true;
}

@Override
public void onShowPress(MotionEvent e) {
    Log.w(this.getClass().toString(),e.toString());
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    Log.w(this.getClass().toString(),e.toString());
    return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    Log.w(this.getClass().toString(),e1.toString());
    return true;
}

@Override
public void onLongPress(MotionEvent e) {
    Log.w(this.getClass().toString(),e.toString());
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    Log.w(this.getClass().toString(),e1.toString());
    return true;
}

I found an example doing this with a deprecated constructor for the GestureListener (new GestureDetector(this)) but i cant do it work either.

What step am i forgetting?


Solution

  • You can use GestureDetector as this:

    // Gesture detection
    gestureDetector = new GestureDetector(this, new MyGestureDetector());
    viewFlipperProduct.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
                v.performClick();
                return gestureDetector.onTouchEvent(event);
        }
    });
    
    private class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {
                return true;
        }
    
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    }