onInterceptTouchEvent的ACTION_UP和ACTION_MOVE永远不会被调用

3

日志没有记录ACTION_UPACTION_MOVE(我在代码示例中删除了它们以缩短长度)。

以下是我简化后的代码:

 public class ProfileBadgeView extends LinearLayout {

    Activity act;

    public ProfileBadgeView(Context context) {
        super(context);
    }

    public ProfileBadgeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProfileBadgeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void initView(Activity act) {
        //..init
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            logIntercept("ACTION DOWN");
        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            logIntercept("ACTION_UP");
        }
        return false;
    }

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


    private void logIntercept(Object obj) {
        Log.i(this.getClass().getSimpleName() + " INTERCEPT :", obj.toString());
    }



}

将 Toast 放在条件之外,检查 Toast 是否显示。 - Umer Farooq
1个回答

12
由于您在onTouchEvent方法中返回了true,因此ACTION_DOWN事件后不会调用onInterceptTouchEvent方法。因此,所有其他事件都将在onTouchEvent中处理而不是在onInterceptTouchEvent中处理:
使用此函数需要注意,因为它与View.onTouchEvent(MotionEvent)有比较复杂的交互,使用它需要以正确的方式实现此方法和onTouchEvent方法。事件将按以下顺序接收:
您将在此处接收到"down"事件。down事件将被该视图组的子元素处理或传递给自己的onTouchEvent()方法进行处理;这意味着您应该实现onTouchEvent()方法并返回true,以便继续看到手势的其余部分(而不是寻找父视图来处理它)。此外,通过从onTouchEvent()返回true,您将不会在onInterceptTouchEvent()中收到任何后续事件,并且所有触摸处理都必须像通常一样在onTouchEvent()中进行。
参考链接:http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接