Android: How to make Swipe screen Navigation comfortable
-
12-10-2019 - |
Question
I am using Gesture Listner to acheive Swipe screen navigation. But it is not so comfortable that I need to swipe multiple time to navigate to next or previous screen. What are the proper values for Velocity, Off path and Min Distance.
Please find below is my code:
private static final int SWIPE_MIN_DISTANCE = 50;
private static final int SWIPE_MAX_OFF_PATH = 300;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
configDisplay();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.showPrevious();
configDisplay();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
Solution 2
These links helped me achieve the solution:
GestureDetector.SimpleOnGestureListener
OTHER TIPS
I recently implemented an app that used flinging and started out with SimpleOnGestureListener just like your example. I logged velocity and offset on each fling and tested on my device to find what I thought was suitable values.
But - I highly recommend you to take a look at the ViewPager which is available in the compability package instead.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow