OnTouchListener on entire Activity

I'm trying to detect a onFling event on the entire area of my activity. To do this I:

1) set the GestureDetectorCompat:

private GestureDetectorCompat gDetect;

2) in OnCreate initialize the detector:

gDetect = new GestureDetectorCompat(this, new GestureListener());

3) override the onTouchEvent:

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

4) create the GestureListener class:

class GestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent event) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2,
                float velocityX, float velocityY) {
        Log.e(null, "onFling: " + event1.toString() + event2.toString());
        return super.onFling(event1, event2, velocityX, velocityY);
    }

}

It works, but it detects onFling gesture ONLY in the part of my UI where there is not any other view. So, if I swipe in a blank area it works, if I swipe in a part of the activity where is another view (for example a ScrollView or a LinearLayout) the detection isn't triggered.

I hope I've explained my problem: how I can detect gesture on the entire surface of the Activity, to accomplish a simple swipe gesture?

Many tanks.


You can use GestureOverlayView. Here is a great starter resource for GestureOverlayView . Also you can have a look at this SO question.

Basically to use this your application must use the view "GestureOverlayView" and you place your other views in this view. Your activity must implement the interface "OnGesturePerformedListener" and if a gesture is detected then "onGesturePerformedListener" method is called.

Hope this helps. Goodluck!

链接地址: http://www.djcxy.com/p/91256.html

上一篇: 没有检测到Fling / Swipe手势

下一篇: 整个Activity上的OnTouchListener