5

I've got a ViewFlipper at the side of the screen in my app containing a bunch of different views, and I would like the user to be able to dismiss this by swiping to the left. So I did the usual...

private class SwipeDetector extends SimpleOnGestureListener {

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        //Dismiss view
    }

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

...and then set the onTouchListener of the ViewFlipper to call onTouchEvent in SwipeDetector. It all worked great, however I then noticed because it was consuming all the touch events going into the ViewFlipper, I couldn't click anything when the ViewFlipper itself. If I don't override onDown or I make it return false then the TouchListener ignores the rest of the event and I don't get the fling. I'm happy to hack together some kind of custom swipe detection if I could even handle all the ACTION_MOVE events following the user touching the ViewFlipper but can't even do that. Is there any way to keep listening to the MotionEvent without consuming the onDown?

Cathal Comerford
  • 1,020
  • 1
  • 9
  • 12

2 Answers2

2

What works for me is extending View.OnTouchListener instead of SimpleGestureListener, and override onTouch method. Please, take this snippet as a very simple (and improvable) approach to get horizontal swipe detection.-

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            downX = event.getX();
            return true;
        }
        case MotionEvent.ACTION_UP: {
            upX = event.getX();
            float deltaX = downX - upX;

            if (Math.abs(deltaX) > MIN_DISTANCE) {
                if (deltaX < 0) {
                    this.onLeftToRightSwipe();
                    return true;
                }
                if (deltaX > 0) {
                    this.onRightToLeftSwipe();
                    return true;
                }
                return true;
            }

            return true;
        }
    }
    return false;
}
ssantos
  • 16,001
  • 7
  • 50
  • 70
  • This handles the touch event just fine but still has the same problem as in my original question - it consumes the touch event and when I set this as the touch listener on the parent view, I can't click anything in the child views because the touch event doesn't get that far. – Cathal Comerford Aug 06 '13 at 11:16
  • @CathalComerford Did you find something? – Mayank Singhal Feb 06 '22 at 11:43
1

Did you notice the GestureDetector? It is available since API Version 1.

http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html

Both fling and scroll gestures are handled.

Stefan Reimers
  • 379
  • 4
  • 9