I am using a viewpager in Android und would like to control which way the user can swipe the page.
I am using this code to detect the direction of the swipe:
// Detects the direction of swipe. Right or left.
// Returns true if swipe is in right direction
public boolean detectSwipeToRight(MotionEvent event){
int initialXValue = 0; // as we have to detect swipe to right
final int SWIPE_THRESHOLD = 100; // detect swipe
boolean result = false;
try {
float diffX = event.getX() - initialXValue;
if (Math.abs(diffX) > SWIPE_THRESHOLD ) {
if (diffX > 0) {
// swipe from left to right detected ie.SwipeRight
result = false;
} else {
Log.e("swipeToLeft", "swipeToLeft");
result = true;
}
}
}
catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
This code however always returns "false". What changes must be made in the code work it to work properly?