setOnTouchListener(new OnTouchListener() {
private float startX, startY, offsetX, offsetY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_UP:
offsetX = event.getX() - startX;
offsetY = event.getY() - startY;
if (Math.abs(offsetX) > Math.abs(offsetY)) {
if (offsetX < -5) { // left
swipeLeft();
} else if (offsetX > 5) { // right
swipeRight();
}
} else {
if (offsetY < -5) { // up
swipeUp();
} else if (offsetY > 5) { // down
swipeDown();
}
}
break;
default:
break;
}
return true;
}
});