Gesture Detector not working

久未见 提交于 2019-12-04 18:02:01

问题


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

回答1:


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.




回答2:


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




回答3:


[view].setLongClickable(true);

This must be called first.



来源:https://stackoverflow.com/questions/5463232/gesture-detector-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!