How to stop scrolling in a Gallery Widget?

I think I found a way to achieve both only scrolling 1 view at a time in the gallery and be able to have a minimal swipe length to trigger animation.

Override the onFling method of the gallery widget and instead of calling super.onFling, check to see whether or not the swipe was from left to right or right to left and call the appropriate dpad event as shown below:

private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
  return e2.getX() > e1.getX();
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
  int kEvent;
  if(isScrollingLeft(e1, e2)){ //Check if scrolling left
    kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
  }
  else{ //Otherwise scrolling right
    kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
  }
  onKeyDown(kEvent, null);
  return true;  
}

Leave a Comment