开发者

GestureOverlayView and Double-Tap

So, I would like to be able to recognize advanced Gestures using the GestureOverlayView and OnGestureListener as well as be able to detect double-taps and long presses using GestureDetector. I have both features working fine separately in two different projects. However, I am trying to combine them and am running into some problems. It seems that when I attempt a double-tap / long press, it is not being recognized because the GestureOverlayView is covering the whole screen and will only recognize the advanced gestures defined in the Gesture Builder. Does anyone know how to set-up the GestureOverlayView so that it will allow GestureDetector to do it's job? My code:

public class HelloAndroid extends Activity implements OnGesturePerformedListener, OnDoubleTapListener, OnGestureListener {
/** Called when the activity is first created. */
private GestureLibrary mLibrary;
private GestureDetector detector;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
    if (!mLibrary.load()) {
        finish();
    }

    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
    gestures.addOnGesturePerformedListener(this);

    detector = new GestureDetector(this, this);\

And the xml...

 <?xml version="1.0" encoding="utf-8"?>

<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gestures"
        android:layout_width="fill_parent" 
        android:visibility="invisible"
        android:layout_height开发者_如何转开发="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:id="@+id/text1"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />  
    </LinearLayout>    
</android.gesture.GestureOverlayView>

Thanks in advance!


I did it, but not sure if my solution will work for you. Do you need to detect double tap and longpress on the whole activity or just in your list?

In my project, what I have is, of course GestureOverlayView wrapping everything, but my GestureDetector was assigned only on the container I want to handle those event, specifically on a Grid with thumbnails.

Actually, my gesture detector is inside in a View.OnTouchListener

public class TouchListener implements View.OnTouchListener {

    Rect rect = new Rect();

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        // We do not use the return value of
        // mGestureDetector.onTouchEvent because we will not receive
        // the "up" event if we return false for the "down" event.
    // false is returned so children views can receive touch event too.
        return false;
}

    private View getViewAt(int x, int y) {
    // I need to know which thumbnail was selected
    }

    GestureDetector gestureDetector = new GestureDetector(new SimpleOnGestureListener() {
        @Override
        public void onLongPress(MotionEvent e) {…}
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {…}
        @Override
        public boolean onDoubleTap(MotionEvent e) {...}

});

}

Then I just need to assign this touch listener to my grid. In your case it may be used on your list.

mGrid.setOnTouchListener(new TouchListener());

What happens is that GesturOverlayView will propagate touch events to inner views while a gesture is not detected. The solution is create a touchlistener on view you need to detect simple gestures using GestureDetector on listener onTouch method. Hope it may help you.


The issue comes from the method onTouchEvent(event) of your activity that is not called because the event is intercepted by the GestureOverlayView.

If you want to get this event in your activity you might want to override the dispatchTouchEvent(event) method.

In your case, this code may work :

@Override
public boolean dispatchTouchEvent(MotionEvent e)
{
    detector.onTouchEvent(e);

    return super.dispatchTouchEvent(e);
}


I was having the same problem.

The GestureOverlayView is intercepting the onTouchEvent. So just set

android:eventsInterceptionEnabled="false"

The result should be something like this:

<android.gesture.GestureOverlayView
        android:id="@+id/gestures"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:eventsInterceptionEnabled="false" >


I was having the some problem. I found a solution but i dont know why it works.

I use dispathTouchEvent method but i call onTouchEvent() with my GestureDetector instance.

@Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
    this.myGestureDetector.onTouchEvent( ev );
    return super.dispatchTouchEvent(ev);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜