Created
May 30, 2017 07:15
-
-
Save Rudis1261/299a4d51bfb9ffeff2206a37e09dcc6b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@Override | |
public boolean onTouchEvent(MotionEvent event) | |
{ | |
switch(event.getAction()) | |
{ | |
case MotionEvent.ACTION_DOWN: | |
x1 = event.getX(); | |
y1 = event.getY(); | |
break; | |
case MotionEvent.ACTION_UP: | |
x2 = event.getX(); | |
y2 = event.getY(); | |
float deltaX = x2 - x1; | |
float deltaY = y2 - y1; | |
// Left / Right | |
if (Math.abs(deltaX) > MIN_DISTANCE) | |
{ | |
// Left to Right swipe action | |
if (x2 > x1) | |
{ | |
Toast.makeText(this, "Left to Right swipe [Next]", Toast.LENGTH_SHORT).show (); | |
} | |
// Right to left swipe action | |
else | |
{ | |
Toast.makeText(this, "Right to Left swipe [Previous]", Toast.LENGTH_SHORT).show (); | |
} | |
} else { | |
// consider as something else - a screen tap for example | |
} | |
// Up / Down | |
if (Math.abs(deltaY) > MIN_DISTANCE) | |
{ | |
// Top to Bottom, (INVERSE IF REVERSED) | |
if (y2 > y1) | |
{ | |
Toast.makeText(this, "Top to Bottom swipe [Next]", Toast.LENGTH_SHORT).show (); | |
} | |
// Bottom to Top | |
else { | |
Toast.makeText(this, "Bottom to Top swipe [Previous]", Toast.LENGTH_SHORT).show (); | |
} | |
} else { | |
// consider as something else - a screen tap for example | |
} | |
break; | |
} | |
return super.onTouchEvent(event); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Might need to reverse the logic on line:40 if the UP / DOWN is reversed.