永远不会调用onInterceptTouchEvent的ACTION_UP和ACTION_MOVE

Ada*_*gyi 1 android

日志永远不会记录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());
    }



}
Run Code Online (Sandbox Code Playgroud)

Don*_*key 7

事件onInterceptTouchEvent之后不会调用您的方法,ACTION_DOWN因为您trueonTouchEvent方法中返回.因此,所有其他事件都被发送,onTouchEvent而不再发送onInterceptTouchEvent:

使用此函数需要注意,因为它与View.onTouchEvent(MotionEvent)的交互相当复杂,并且使用它需要以正确的方式实现该方法以及此方法.活动将按以下顺序收到:

您将在这里收到羽绒服.down事件将由此视图组的子进程处理,或者由您自己的onTouchEvent()方法处理; 这意味着你应该实现onTouchEvent()以返回true,这样你将继续看到手势的其余部分(而不是寻找父视图来处理它).此外, 通过从onTouchEvent()返回true,您将不会在onInterceptTouchEvent()中收到任何后续事件,并且所有触摸处理必须像on normal一样在onTouchEvent()中进行.

http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)