2 * Copyright © 2018 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui;
20 import android.content.Context;
21 import android.util.Log;
22 import android.view.GestureDetector;
23 import android.view.GestureDetector.SimpleOnGestureListener;
24 import android.view.MotionEvent;
25 import android.view.View;
27 public abstract class OnSwipeTouchListener implements View.OnTouchListener {
28 public final GestureDetector gestureDetector;
30 protected OnSwipeTouchListener(Context ctx) {
31 gestureDetector = new GestureDetector(ctx, new GestureListener() );
34 private final class GestureListener extends SimpleOnGestureListener {
35 private static final int SWIPE_THRESHOLD = 100;
36 private static final int SWIPE_VELOCITY_THRESHOLD = 100;
39 public boolean onDown(MotionEvent e) {
40 Log.d("sw-l", "onDown");
45 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
46 boolean result = false;
48 Log.d("sw-l", "onFling");
51 float diffX = e2.getX() - e1.getX();
52 float diffY = e2.getY() - e1.getY();
53 if (Math.abs(diffX) > Math.abs(diffY)) {
54 if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
56 Log.d("sw-l", "calling onSwipeRight");
60 Log.d("sw-l", "calling onSwipeLeft");
66 else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
84 public void onSwipeRight() {}
85 public void onSwipeLeft() {
86 Log.d("sw-l", "LEFT");
88 public void onSwipeUp() {}
89 public void onSwipeDown() {}