I have set up my Activity to have a gestureDetectorCompat that detects left and right swipes, but my Activity also has a horizontal RecyclerView in it.
When I try to scroll the RecyclerView Activity getsureDetector triggers too, which is undesirable.
Here is the code related to gestureDetector if it helps.
private GestureDetectorCompat mDetector;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
mDetector = new GestureDetectorCompat(this, new SwipeDetector());
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
public boolean dispatchTouchEvent(MotionEvent ev){
if (mDetector != null){
if (mDetector.onTouchEvent(ev)){
return true;
}
}
return super.dispatchTouchEvent(ev);
}
public class SwipeDetector extends GestureDetector.SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
return false;
}
if( e2.getX() > e1.getX() ){
if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){
Left();
return true;
}
}
if( e1.getX() > e2.getX() ){
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){
Right();
return true;
}
}
return false;
}
}