In my android app I am using onFling() method of onTouchListenerSimpleOnGestureListener to detect swipe left, top, right, bottom.
Problem is, when I swipe up it also triggers swipe left or right.
I tried playing around with SWIPE_THRESHOLD value but it didn't make a difference even when I increased it to 5000 from 50.
How can I make sure that only swipe up will be fired when I swipe top.
public class OnSwipeTouchListener implements OnTouchListener
{
public static final int SWIPE_THRESHOLD = 50;
public static final int SWIPE_VELOCITY_THRESHOLD = 50;
protected GestureDetector gestureDetector;
final int IDLE = 0;
final int TOUCH = 1;
final int PINCH = 2;
int touchState;
double dist0, distCurrent;
private Context CONTEXT;
public OnSwipeTouchListener(Context ctx)
{
gestureDetector = new GestureDetector(ctx, new GestureListener());
CONTEXT = ctx;
}
public OnSwipeTouchListener() {
}
public void onSwipeRight()
{
}
public void onSwipeLeft()
{
}
public void onSwipeTop(float diffY)
{
}
public void onSwipeBottom(float diffY)
{
}
public void onDoubleTap(){
}
// public abstract void onSwipeRight();
//
// public abstract void onSwipeLeft() ;
//
// public abstract void onSwipeTop(double diff) ;
//
// public abstract void onSwipeBottom(double diff);
//
// public abstract void onPinch(double distCurrent) ;
@Override
public boolean onTouch(View v, MotionEvent event)
{
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
boolean result = false;
try
{
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if(Math.abs(diffX) > Math.abs(diffY))
{
if(Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
{
if(diffX > 0)
{
onSwipeRight();
}
else
{
onSwipeLeft();
}
}
result = true;
}
else if(Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD)
{
if(diffY > 0)
{
onSwipeBottom(diffY);
}
else
{
onSwipeTop(diffY);
}
result = true;
}
}
catch(Exception exception)
{
exception.printStackTrace();
}
return result;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
OnSwipeTouchListener.this.onDoubleTap();
return super.onDoubleTap(e);
}
}
}
EDIT : What I want to achieve is, when the user swipes up then swipe left or swipe right should not get fired/ called.(which are being called right now when I swipe up)
EDIT 2 : I have changed the onFling() method to just use velocities and upon debugging it seems its working correctly i.e. It does not fire swipeLeft() or swipeRight() when I swipeTop()
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
boolean result = false;
try
{
if(Math.abs(velocityX) > Math.abs(velocityY)){
//Horizontal Swipe
if(velocityX < 0){
//Swipe Left
onSwipeLeft();
result = true;
}
else if(velocityX > 0){
//Swipe Right
onSwipeRight();
result = true;
}
}
else if(Math.abs(velocityY) > Math.abs(velocityX)){
//Vertical Swipe
if(velocityY < 0){
//Swipe Top
onSwipeTop(Math.abs(e1.getY() - e2.getY()));
result = true;
}
else if(velocityY > 0){
//Swipe Bottom
onSwipeBottom(Math.abs(e1.getY() - e2.getY()));
result = true;
}
}
}
catch(Exception exception)
{
exception.printStackTrace();
}
return result;
}
Now here is the problem.
I am using library https://github.com/lsjwzh/RecyclerViewPager and set onTouchListener with GestureDetector with above onFling() method.
Even though I have set a custom onTouchListener it seems the RecyclerViewPager seems to be getting swipe commands/ gestures after the custom onFling() method.
I tried setting recyclerViewPager.setOnFlingListener(null) but it did not work.
How do I disable gestures being passed to recyclerViewPager after setting custom onTouchListener?