【问题标题】:Handling motion event many times多次处理运动事件
【发布时间】:2013-07-12 16:11:13
【问题描述】:

我有两个视图:一个子视图和一个父视图(子视图是父视图的一部分);每个 View 都有自己的 onTouchListener。我希望每个视图都处理自己的 onInterceptTouchEvent。马上;但是,子视图的 onTouchListener 永远不会收到任何事件。它们都被发送到父视图的监听器。如何确保子视图接收到自己的事件?

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    //Log.d(TAG, "onInterceptTouchEvent " + ev);
    // handle child events
    // note: if you horizontally fling over button its onClick() is not performed


        MotionEvent ss  = MotionEvent.obtain(ev);
        globalVariabels.currentGallery.onTouchEvent(ss);
        int bounds[] = {0,0};
        globalVariabels.currentGallery.getLocationOnScreen(bounds);
        int height = globalVariabels.currentGallery.getHeight() + bounds[0];
        int width = globalVariabels.currentGallery.getWidth() + bounds[1];
        TouchDelegate delegate = new TouchDelegate(new Rect(0, 0, 2000, 2000), globalVariabels.currentGallery); 
         setTouchDelegate(delegate);
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                mIgnore = false;
                Log.e("ooo", "oo");
                Log.e("X:","" + ev.getX());
                Log.e("Y:","" + ev.getY());
                mNeedToRebase = true;
                mInitialX = ev.getX();
                mInitialY = ev.getY();
                return false ;
            }

            case MotionEvent.ACTION_MOVE: {
                if (!mIgnore) {
                    Log.e("22", "22");
                    float deltaX = Math.abs(ev.getX() - mInitialX);
                    float deltaY = Math.abs(ev.getY() - mInitialY);
                    mIgnore = deltaX < deltaY;
                    super.onInterceptTouchEvent(ev);
                    return !mIgnore;
                }
                return false;
            }
            default: {
                return super.onInterceptTouchEvent(ev);
            }
        }
    }

【问题讨论】:

  • 你的问题是......?
  • 当我尝试滑动孩子时,事件由父母处理。
  • 在这里查看我的答案:stackoverflow.com/questions/16979285/…
  • 但我还有另一个 onInterceptTouchEvent
  • 我不懂你

标签: android


【解决方案1】:

考虑使用dispatchTouchEvent 而不是onInterceptTouchEvent。我遇到了一些问题,因为在某些设备上(更准确地说,在三星上)它可能无法按预期工作。而且调度真的很简单。

想法是您将覆盖 dispatchTouchEvent,调用 super 并处理您的运动事件。这是一个例子:

@Override
protected boolean dispatchTouchEvent(MotionEvent ev) {
    //perform your on-touch behaviour

    return super.dispatchTouchEvent(ev);    //child's onTouchListener will be triggered
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 2011-01-15
    • 2011-12-12
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多