0

Is there a way that i could handle scroll up and down in my activities thank's to Gesture.SWIPE_RIGHT(which will trigger the scroll up action) and Gesture.SWIPE_LEFT(which will trigger the scroll down action) ?

Any piece of code will be much appreciate...

1 Answers1

0

The following code is basically copy pasted from the official guide. Define the scrollUp and scrollDown methods as you wish.

public class MyActivity extends Activity {
  private GestureDetector mGestureDetector;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    ...
    mGestureDetector = createGestureDetector();
  }

  @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        if (mGestureDetector != null) {
            return mGestureDetector.onMotionEvent(event);
        }
        return false;
    }

  private GestureDetector createGestureDetector() {
    GestureDetector gestureDetector = new GestureDetector(this);
    gestureDetector.setBaseListener(new GestureDetector.BaseListener() {
      @Override
      public boolean onGesture(Gesture gesture) {
        if (gesture == Gesture.SWIPE_RIGHT) {
          scrollUp();
          return true;
        } else if (gesture == Gesture.SWIPE_LEFT) {
          scrollDown();
          return true;
        }
        return false;
      }
    }

    return gestureDetector;
  }

}
Jakub K
  • 1,713
  • 1
  • 13
  • 21
  • Thank you for your answer ! but this is precisely my issue: how to define scrollUp() and scrollDown()... I have search an easy way to implement those methods but i didn't succeed – user3797119 Aug 01 '14 at 13:25