1

I'm trying to detect when an swipe direction was changed while the user still swipes on the screen.

I have something like this (very basic) for detecting the swipe direction:

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    int action = motionEvent.getActionMasked();

    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            Log.d(TAG, "onTouch: DOWN _Y = " + motionEvent.getRawY());
            mLastTouchY = mPrevTouchY = motionEvent.getRawY();

            break;
        }
        case MotionEvent.ACTION_MOVE: {
            Log.d(TAG, "onTouch: MOVE _Y = " + motionEvent.getRawY());

            final float dy = motionEvent.getRawY();
            if (dy >= mLastTouchY) {
                /* Move down */

            } else {
                /* Move up */

            }

            break;
        }
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_OUTSIDE:
        case MotionEvent.ACTION_UP: {
            Log.d(TAG, "onTouch: UP _Y = " + motionEvent.getRawY());

            // snap page

            break;
        }
    }

    return true;
}

What I need is to actually detect when the user changed the direction of the swipe. For example, the code above fails to detect some edge cases:

  1. Start from Y = 100,
  2. move down until 150,
  3. move up until 50,
  4. move down again until 90

This will be detected as an swipe up because initial Y is higher than the last Y

Ionut Negru
  • 6,186
  • 4
  • 48
  • 78
  • If you start from y = 100, then how would you move UP until y = 150? – Saeed Entezari Jul 25 '16 at 15:29
  • Sorry, I messed up directions, it is updated now. – Ionut Negru Jul 25 '16 at 16:59
  • What you get is actually true... . If you want just the last direction of the swipe (What i understand from the question) you have to implement it differently. – Saeed Entezari Jul 25 '16 at 17:16
  • I know it is basically true, but when the user changes the direction I want to react correctly to that change. The problem is that I couldn't come up with a good solution for detecting when an change of direction actually happened with the OnTouch MotionEvent. If you have any idea please post it, maybe it can at least guide me in the right direction. Thank you. – Ionut Negru Jul 26 '16 at 09:52

1 Answers1

0

Should you want to detect the direction change of the swipe there is a simple way:

    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        findViewById(R.id.myView).setOnTouchListener(this);
        gestureDetector = new GestureDetector(this, this);
    }

And you implement OnTouch and GestureListeners like this:

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        if (distanceY > 0){
            // you are going up
        } else {
            // you are going down
        }
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
    //the rest of the methods you must implement...
Saeed Entezari
  • 3,685
  • 2
  • 19
  • 40
  • I will try to switch to GestureDetector, maybe it is easier to achieve this. Do you know if this correctly detects when you go out of bounds ? – Ionut Negru Jul 26 '16 at 09:53
  • @IonutNegru Not sure it does that. You can always test it though ;) – Saeed Entezari Jul 26 '16 at 12:45
  • will test it the following days because currently I'm caught up with other tasks, but as soon as I do I'll get back with the result. Thank you. – Ionut Negru Jul 26 '16 at 15:46