我希望能够在屏幕上的任意位置滑动以调用某个功能。但我也想单击Buttons中的Linear Layouts。如果我在Button上滑动,则希望onInterceptTouchEventInterceptButton进行调用,然后执行滑动操作。而且,如果我仅单击onTouchEvent,则不希望调用Button。相反,我希望调用onInterceptTouchEventButton并执行onTouchEvent
但是当我尝试实现Button click时出现错误。

这是我的代码:

public class Game extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_activity);

   //other code....
}
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            swipeScreen(); //if action recognized as swipe then swipe
            break;
        case MotionEvent.ACTION_MOVE:
            float x = event.getX();
            float y = event.getY();
            float xDelta = Math.abs(x - mLastX);
            float yDelta = Math.abs(y - mLastY);

            if (yDelta > xDelta) {
                return true;
            }
            break;
    }

    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    ButtonOnClick(); //if not a swipe, then button click
    return true;
}

首先错误说:onInterceptTouchEvent
然后,我将代码改为The method onInterceptTouchEvent(MotionEvent) of type Game must override or implement a supertype method,而不是return true,但随后出现错误消息:return super.onInterceptTouchEvent(event)
有人可以帮忙吗?

最佳答案

请注意, onInterceptTouchEvent() ViewGroup类的方法,而不是Activity的方法。

您可以通过将逻辑从onInterceptTouchEvent()移到 dispatchTouchEvent(MotionEvent ev) 来实现所需的行为。记住要调用dispatchTouchEvent(MotionEvent ev)的父类(super class)实现来处理应该正常处理的事件。

另请注意,仅当增量大于system constant for touch slop时,才应将 Action 视为滑动。并且我建议通过测试yDelta / 2 > xDelta而不是yDelta > xDelta来确保用户朝着您想要的方向滑动。

public class Game extends Activity {
    private int mSlop;
    private float mDownX;
    private float mDownY;
    private boolean mSwiping;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_activity);

        ViewConfiguration vc = ViewConfiguration.get(this)
        mSlop = vc.getScaledTouchSlop();

       //other code....
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownX = ev.getX();
                mDownY = ev.getY();
                mSwiping = false;
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                if(mSwiping) {
                    swipeScreen(); //if action recognized as swipe then swipe
                }
                break;
            case MotionEvent.ACTION_MOVE:
                float x = ev.getX();
                float y = ev.getY();
                float xDelta = Math.abs(x - mDownX);
                float yDelta = Math.abs(y - mDownY);

                if (yDelta > mSlop && yDelta / 2 > xDelta) {
                    mSwiping = true;
                    return true;
                }
                break;
        }

        return super.dispatchTouchEvent(ev);
    }
}

09-30 11:20