5

更新:我知道发生了什么。看评论。

我正在尝试编写一个 ViewSwitcher,它将所有手势传递给它的第一个孩子,直到它收到一个缩放手势;然后它将它们传递给它的第二个孩子,直到该孩子再次完全缩小,然后它恢复到第一个孩子。我的子类有一个 ScaleGestureDetector,我做了一个非常简单的监听器:

    protected class OnScaleModeSwitcher implements ScaleGestureDetector.OnScaleGestureListener
    {
        protected PageFlipSwitcher owner;

        public OnScaleModeSwitcher(PageFlipSwitcher newOwner)
        {
            super();
            owner = newOwner;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            return false;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            owner.onScaleBegin();
//returning false here causes the rest of the gesture to be ignored.
            return false;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            owner.onScaleEnd();
        }
    }

如您所见,它所做的只是在构造时引用所有者对象,然后将一些事件传递给所有者类中的方法。但是,代码未达到 onScaleEnd() 。

我知道 onInterceptTouchEvent 可能有点冒险。我尽可能地遵循了 Android 文档中的建议,并且

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        onTouchEvent(ev);
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
//mode is the name of the ScaleGestureDetector
        mode.onTouchEvent(ev);

//this code just passes events to the children
//it seems to work OK
        if(zoomActive)
        {
            //ZoomSwitcher
            getChildAt(1).onTouchEvent(ev);
        }
        else
        {
            //Gallery
            getChildAt(0).onTouchEvent(ev);
        }
        return true;
    }

I read somewhere else that a GestureDetector may not receive the ACTION_UP event:

Android: How to detect when a scroll has ended

Is that what is happening here? If so what is the point of the onScaleEnd() method?

EDIT:

I have worked this out: it is because my methods return false. Eclipse auto-implemented some stubs and I didn't change the return values when I filled them in.

4

1 回答 1

7

If a ScaleGestureDetector is set up that returns false from onScaleBegin(...), none of the subsequent methods will be hit. Generally methods that consume a MotionEvent but return false do not get subsequent MotionEvents until after ACTION_UP, when the listeners are reset.

于 2011-07-27T12:10:56.953 回答