Created
May 3, 2017 17:41
-
-
Save Cassie-von-Clausewitz/63904d6466b4f60062830642b4cfbd66 to your computer and use it in GitHub Desktop.
OnGestureEvent implimentations for use with GestureDetectorCompat. Makes it easer to use lambdas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.GestureDetector; | |
import android.view.MotionEvent; | |
import com.fernandocejas.arrow.functions.Function; | |
import com.fernandocejas.arrow.functions.Predicate; | |
public class GestureDetectors { | |
public static class OnDown implements GestureDetector.OnGestureListener { | |
private final Predicate<MotionEvent> predicate; | |
public OnDown(Predicate<MotionEvent> predicate) { | |
this.predicate = predicate; | |
} | |
@Override | |
public boolean onDown(MotionEvent e) { | |
return predicate != null && predicate.apply(e); | |
} | |
@Override | |
public void onShowPress(MotionEvent e) { | |
} | |
@Override | |
public boolean onSingleTapUp(MotionEvent e) { | |
return false; | |
} | |
@Override | |
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { | |
return false; | |
} | |
@Override | |
public void onLongPress(MotionEvent e) { | |
} | |
@Override | |
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { | |
return false; | |
} | |
} | |
public static class OnShowPress implements GestureDetector.OnGestureListener { | |
private final Function<MotionEvent, Void> function; | |
public OnShowPress(Function<MotionEvent, Void> function) { | |
this.function = function; | |
} | |
@Override | |
public boolean onDown(MotionEvent e) { | |
return false; | |
} | |
@Override | |
public void onShowPress(MotionEvent e) { | |
function.apply(e); | |
} | |
@Override | |
public boolean onSingleTapUp(MotionEvent e) { | |
return false; | |
} | |
@Override | |
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { | |
return false; | |
} | |
@Override | |
public void onLongPress(MotionEvent e) { | |
} | |
@Override | |
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { | |
return false; | |
} | |
} | |
public static class OnSingleTapUp implements GestureDetector.OnGestureListener { | |
private final Predicate<MotionEvent> predicate; | |
public OnSingleTapUp(Predicate<MotionEvent> predicate) { | |
this.predicate = predicate; | |
} | |
@Override | |
public boolean onDown(MotionEvent e) { | |
return false; | |
} | |
@Override | |
public void onShowPress(MotionEvent e) { | |
} | |
@Override | |
public boolean onSingleTapUp(MotionEvent e) { | |
return predicate != null && predicate.apply(e); | |
} | |
@Override | |
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { | |
return false; | |
} | |
@Override | |
public void onLongPress(MotionEvent e) { | |
} | |
@Override | |
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { | |
return false; | |
} | |
} | |
public static class OnLongPress implements GestureDetector.OnGestureListener { | |
private final Function<MotionEvent, Void> function; | |
public OnLongPress(Function<MotionEvent, Void> function) { | |
this.function = function; | |
} | |
@Override | |
public boolean onDown(MotionEvent e) { | |
return false; | |
} | |
@Override | |
public void onShowPress(MotionEvent e) { | |
} | |
@Override | |
public boolean onSingleTapUp(MotionEvent e) { | |
return false; | |
} | |
@Override | |
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { | |
return false; | |
} | |
@Override | |
public void onLongPress(MotionEvent e) { | |
function.apply(e); | |
} | |
@Override | |
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment