Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: detecting fling gestures

In my app I try to capture a fling event on a view (a ScrollView to be exact, but tried with LinearLayout as well).

By setting breakpoints I can see the that the MotionEvents happen correctly up to the point where the onFling() should fire.

The event flow is as follows:

  1. The view's onTouchEvent
  2. The OnGestureListener's onDown
  3. The OnGestureListener's onShowPress
  4. The OnGestureListener's onLongPress

I'm at a loss on how to go on trying to debug something like that and Google search does not turn up much. Any pointers ?

I also attach the relevant view that should handle the gestures:

import android.content.Context;
import android.content.Intent;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class GestureScrollView extends ScrollView {
    private static int MAJOR_MOVE = 60;
    private GestureDetector gd;

    public GestureScrollView(final Context context) {
        super(context);
        gd = new GestureDetector(context,
                new GestureDetector.OnGestureListener() {

                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public void onShowPress(MotionEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                            float distanceY) {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public void onLongPress(MotionEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                            float velocityY) {
                        int dx = (int) (e2.getX() - e1.getX());
                        if (Math.abs(dx) > MAJOR_MOVE
                                && Math.abs(velocityX) > Math.abs(velocityY)) {
                            if (velocityX < 0) {
                                Intent intent = new Intent(context, Main.class);
                                context.startActivity(intent);
                            }
                        }
                        return false;
                    }

                    @Override
                    public boolean onDown(MotionEvent e) {
                        // TODO Auto-generated method stub
                        return false;
                    }
                });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gd.onTouchEvent(event);
    }
}
like image 773
javito Avatar asked Dec 15 '09 21:12

javito


People also ask

What is fling gesture in Android?

Fling-based animation uses a friction force that is proportional to an object's velocity. Use it to animate a property of an object and to end the animation gradually. It has an initial momentum, which is mostly received from the gesture velocity, and gradually slows down.

What is gesture detection app?

GestureDetector makes it easy for you to detect common gestures without processing the individual touch events yourself.

What is onFling?

onFling: is that the user lifts his finger in the end of the movement (that is the reason for what onFling is called one time).


1 Answers

Take the MotionEvent from Activity not from your View. When your Gesture detector is based on MotionEvent that comes from Activity you will get the onFling event. So you have to move onTouchEvent() from your GestureScrollView to your Activity class. Regards!

like image 54
Ramps Avatar answered Nov 10 '22 00:11

Ramps