Gesture Detector not working

Marcos Vasconcelos picture Marcos Vasconcelos · Mar 28, 2011 · Viewed 11.1k times · Source

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;
        }
    }

Answer

bigstones picture bigstones · Mar 28, 2011

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.