onInterceptTouchEvent的ACTION_UP和ACTION_MOVE不会被调用 [英] onInterceptTouchEvent's ACTION_UP and ACTION_MOVE never gets called

查看:431
本文介绍了onInterceptTouchEvent的ACTION_UP和ACTION_MOVE不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

日志从未登录 ACTION_UP ACTION_MOVE (这是我从缩短code示例中删除)

下面是我的code的缩短版:

 公共类ProfileBadgeView扩展的LinearLayout {    活动的行为;    公共ProfileBadgeView(上下文的背景下){
        超级(上下文);
    }    公共ProfileBadgeView(上下文的背景下,ATTRS的AttributeSet){
        这(背景下,ATTRS,0);
    }    公共ProfileBadgeView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
    }    公共无效initView(活动行为){
        //..在里面
    }
    @覆盖
    公共布尔onInterceptTouchEvent(MotionEvent EV){        如果(ev.getAction()== MotionEvent.ACTION_DOWN){
            logIntercept(ACTION DOWN);
        }否则如果(ev.getAction()== MotionEvent.ACTION_UP){
            logIntercept(ACTION_UP);
        }
        返回false;
    }    @覆盖
        公共布尔onTouchEvent(MotionEvent EV){
        返回true;
}
    私人无效logIntercept(obj对象){
        Log.i(this.getClass()getSimpleName()+INTERCEPT:,obj.toString());
    }}


解决方案

onInterceptTouchEvent 方法不经过 ACTION_DOWN 事件,因为你在 onTouchEvent 方法返回真正。因此,所有其他的事件在 onTouchEvent 发送,而不是在 onInterceptTouchEvent 更多:


  

使用此功能需要一些护理,因为它有一个相当复杂的
  与View.onTouchEvent(MotionEvent)相互作用,以及使用它需要
  实施该方法,以及这一个正确的方式。
  活动将在下面的顺序来接收


  
  

您将在这里接收下来的事件。 Down事件将被处理
  无论是这个观点组的孩子,或者给自己
  onTouchEvent()方法来处理;这意味着你应该实现
  onTouchEvent()返回true,所以你会继续看到剩下
  手势(而不是寻找一个父视图处理它)。也,
  由onTouchEvent()返回true,您将不会收到任何
  继onInterceptTouchEvent()事件和全触控处理
  在onTouchEvent()像正常
必须发生。


<一个href=\"http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)\">http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)

Log is never logging ACTION_UP or ACTION_MOVE(which i removed from the code example for shortening)

Here is my shorten version of the code:

 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());
    }



}

解决方案

Your onInterceptTouchEvent method is not called after ACTION_DOWN event because you return true in onTouchEvent method. So all the other events are sent in onTouchEvent and not in onInterceptTouchEvent any more:

Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:

You will receive the down event here. The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.

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

这篇关于onInterceptTouchEvent的ACTION_UP和ACTION_MOVE不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆