Android OnGestureListener Fling is not detecting
Asked Answered
U

3

11

I want to detect fling motion in a block of the screen. I am using the following code for that.

public class MyinfoActivity extends Activity implements OnGestureListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        ..........
        ..........
        gestureScanner = new GestureDetector(this);
        resBlock = (TableRow) findViewById(R.id.ResBlock);
        gestureScanner = new GestureDetector(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent me){
        Log.d(null,"Touch");
        if (gestureScanner.onTouchEvent(me))
            return gestureScanner.onTouchEvent(me);
        else
            return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
                       float velocityX, float velocityY) {
        Log.d(null,"Fling");
        ............
        ............
    }

    @Override
    public boolean onDown(MotionEvent arg0) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {}

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                        float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {}

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

It is detecting the TouchEvent, but it is not detecting any fling motion. What is the problem in my code?

Unman answered 15/3, 2011 at 9:46 Comment(0)
U
13

I used the following code and solved the issue.

public class MyinfoActivity extends Activity {
    private GestureDetector gestureScanner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myinfotrackerinner);
        gestureScanner = new GestureDetector(this,simpleOnGestureListener);
    }

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

    GestureDetector.SimpleOnGestureListener simpleOnGestureListener =
                       new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDown(MotionEvent event) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent event1, MotionEvent event2,
                                   float velocityX, float velocityY) {
            Log.d(null,"Fling");
            int dx = (int) (event2.getX() - event1.getX());
            // don't accept the fling if it's too short
            // as it may conflict with a button push
            if (Math.abs(dx) > MAJOR_MOVE 
                       && Math.abs(velocityX) > Math.abs(velocityY)) {
                if (velocityX > 0) {
                    moveGraph("L");
                } else {
                    moveGraph("R");
                }
                return true;
            } else {
                return false;
            }
        }
    };
}
Unman answered 17/3, 2011 at 13:4 Comment(2)
Thanks. This post helped me a lot. The key here is to return true in the onDown(MotionEvent) method!Gatling
and what value would MAJOR_MOVE have? ;)Permit
A
3

First your onTouchEvent method is incorrect and will cause 2 call of onTouchEvent by the gestureScanner object. You need to change by this :

    @Override
    public boolean onTouchEvent(MotionEvent me){
         Log.d(null,"Touch");
         if (gestureScanner.onTouchEvent(me))
             return true;
         else
             return super.onTouchEvent(event); // or false (it's what you whant).
    }

You also have this line twice :

gestureScanner = new GestureDetector(this);

Then try to change your onFling method like this :

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
    try{
        if(Math.abs(e1.getY()-e2.getY()) > 250) 
            return false;               
        if(e1.getX() - e2.getX() > 120 && Math.abs(velocityX) > 200){
            Log.d("Fling", "Move Next");
            //do something...
        }
        else if(e2.getX() - e1.getX() > 120 && Math.abs(velocityX) > 200){
            Log.d("Fling", "Move Previous");
            //do something...
        }
        return false;
    }
    catch(Exception e){
        return false;
    }
}
Aberration answered 15/3, 2011 at 10:1 Comment(1)
I have tried by changing code of onTouchEvent as per direction and also the onFling method. But no change in the result. It is still detecting the touch event, but no onFling event.Unman
M
3

return true in your onDown method

@Override
public boolean onDown(MotionEvent arg0) {
    return true;
}
Morganatic answered 20/8, 2014 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.