Android onGestureListener and onTouchListner

I am working in android. I have some functions to be done by following methods: -

1. MotionEvent.ACTION_DOWN
2. MotionEvent.ACTION_UP
3. MotionEvent.ACTION_MOVE

      

and I also want to render fling()

in this image.

For the above requirement, I realized onGestureListener

, and onTouchListener

in its application, but they do not work properly. onTouchListener

works but onGestureListener

doesn't work. When I remove the code onTouchListner

then it onGestureListener

works correctly.

So please suggest to me what should I do for this. I want to implement these four methods in my application.

1. MotionEvent.ACTION_DOWN
2. MotionEvent.ACTION_UP
3. MotionEvent.ACTION_MOVE
4. onFling

      

+3


source to share


2 answers


You can achieve this by executing an OnGestureListener from your activity, overriding the methods below.

// For touch 
public boolean onSingleTapUp(MotionEvent event) { return false; }

// For Fling 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    return false;
}

      

Hope it helps.


EDIT 1 : Detailed explanation:

1> implement OnGestureListener from your activity

public class MyActivity implements OnGestureListener

      

2> create an instance of GestureDetector:



private GestureDetector gestureScanner;

      

And in onCreate

:

// Avoid a deprecated constructor 
// (http://developer.android.com/reference/android/view/GestureDetector.html)
gestureScanner = new GestureDetector(this, this);

      

3> Cancel the following methods:

@Override
public boolean onTouchEvent(MotionEvent event) {
    return gestureScanner.onTouchEvent(event);
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    /* on scroll to the next page in story */

    if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        // ...
    }
    /* on scroll to the previous page in story */
    else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        // ...
    }

    return false;
}

@Override
public boolean onSingleTapUp(MotionEvent event) {
    return false;
}

      


EDIT 2 : To handle Move, Override onScroll method for details here

+7


source


As advice coming from another android developer who was chasing the primrose path from swipe or fling event ... When writing a listener or trying to implement an OnGestureListener class, be sure to in your import. import android.view.GestureDetector.OnGestureListener



Otherwise OnGesturListener

will use import GestureOverlayView and this is not what you think you want. This is especially useful when trying to implement gesture capturing using the Gestures app and importing saved gestures and creating a gesture library for your app at runtime so that you can then "predict" what the user is doing by running some fantastic algorithm in the background and seeing less total deviates from user input. Which turns out to be a bunch of locations that came from the OS when dragging little little letters onto a flat screen ... This is a different approach than just trying to capture a throw or long press. Which looks like what users are looking for above, rather than some fancy multi-press gesture capture, which makes this approach really easy.This method is also very useful in video games and gives possibly a backdoor to any people who love developing games. Or in a 9-5 world to put a support or admin backdoor in your mobile app to hide some settings or logs from regular users. In this case, you can also use the above methods, but then you have to get an array of points or locations all over the screen and it becomes tedious for any non-mathematicians or developers learning on their own. Hope I helped someone or helped. Nobody mentioned this to me when I was researching it and I never found it in any of the several books I read about Android.to put a support or admin backdoor in your mobile app to hide some settings or logs from regular users. In this case, you can also use the above methods, but then you have to get an array of points or locations all over the screen and it becomes tedious for any non-mathematicians or developers learning on their own. Hope I helped someone or helped. Nobody mentioned this to me when I was researching it and I never found it in any of the several books I read about Android.to put a support or admin backdoor in your mobile app to hide some settings or logs from regular users. In this case, you can also use the above methods, but then you have to get an array of points or locations all over the screen and it becomes tedious for any non-mathematicians or developers learning on their own. Hope I helped someone or helped. Nobody mentioned this to me when I was researching it and I never found it in any of the several books I read about Android.and it becomes tedious for any non-mathematician or self-taught developer. Hope I helped someone or helped. Nobody mentioned this to me when I was researching it and I never found it in any of the several books I read about Android.and it becomes tedious for any non-mathematician or self-taught developer. Hope I helped someone or helped. Nobody mentioned this to me when I was researching it and I never found it in any of the several books I read about Android.

0


source







All Articles