Skip to content

Instantly share code, notes, and snippets.

@thrashedbrain
Created January 8, 2021 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thrashedbrain/7172f35b198474c167379a4a5403e18c to your computer and use it in GitHub Desktop.
Save thrashedbrain/7172f35b198474c167379a4a5403e18c to your computer and use it in GitHub Desktop.
public class SwipeGestureDetector implements View.OnTouchListener {
private GestureDetector gestureDetector;
public SwipeGestureDetector(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public SwipeGestureDetector() {
}
public void onSwipeUp() {
}
public void onSwipeDown() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceY) > Math.abs(distanceX) && Math.abs(distanceY) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceY > 0)
onSwipeDown();
else
onSwipeUp();
return true;
}
return false;
}
}
}
linearLayout.setOnTouchListener(new HoroscopeGestureDetector(context){
@Override
public void onSwipeDown() {
//do something
super.onSwipeDown();
}
@Override
public void onSwipeUp() {
//do something
super.onSwipeUp();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment