Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

Gesture Detector not working

I has the following GestureListener:

public class BookListener extends SimpleOnGestureListener implements
        OnTouchListener {
    private LibraryActivity main;
private Book book;
private GestureDetector gesture;

public BookListener(Book book, LibraryActivity main) {
    this.main = main;
    this.book = book;
    gesture = new GestureDetector(main,this);
}

public boolean onDoubleTap(MotionEvent e) {
    main.showInfo(book);
    return true;
}

public boolean onSingleTapConfirmed(MotionEvent e) {
    main.openBook(book.getUrl());
    return true;
}

public boolean onDown(MotionEvent evt){
    return false;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    return gesture.onTouchEvent(event);
    }
}

I add it to my View by this way:

view.setOnTouchListener(new BookListener(book, main));

But when running, the events are not triggered, I debug it, I see the onDown is called, but onSingleTapConfirmed or onDoubleTap nevers works.

What's wrong?

I has this code with works perfectly:

    private class GestureListener extends SimpleOnGestureListener {
        private boolean newEvent = true;
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            document.rescale();
            refreshImage();
            return true;
        }

        public boolean onDown(MotionEvent evt){
            newEvent = true;
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            if (!document.isScaled() && newEvent) {
                Vector2D v = new Vector2D(e2).minus(new Vector2D(e1));
                if (v.getX() > 60 || v.getX() < -60){
                    if (v.getX() < 0)
                        next();
                    else
                        previous();
                    newEvent = false;
                }
            } else {
                img.notifyScroll(-distanceX, -distanceY);
                img.invalidate();
            }
            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            switchSideBar();
            return true;
        }
    }

Upvotes: 13

Views: 11967

Answers (3)

Shashank Tomar
Shashank Tomar

Reputation: 862

Whether or not you use GestureDetector.OnGestureListener, it's best practice to implement an onDown() method that returns true. This is because all gestures begin with an onDown() message. If you return false from onDown(), as GestureDetector.SimpleOnGestureListener does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods of GestureDetector.OnGestureListener never get called. This has the potential to cause unexpected problems in your app. The only time you should return false from onDown() is if you truly want to ignore an entire gesture.

http://developer.android.com/training/gestures/detector.html#detect

Upvotes: 5

Sin Sangdo
Sin Sangdo

Reputation: 31

[view].setLongClickable(true);

This must be called first.

Upvotes: 3

bigstones
bigstones

Reputation: 15257

onDown() must return true even if you don't want to react to that event, or else it will make the detector discard any following event and hence any gesture.

Upvotes: 43

Related Questions