gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:38:28 27 4
gpt4 key购买 nike

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



}

最佳答案

onInterceptTouchEvent 方法不会在 ACTION_DOWN 事件后调用,因为您在 onTouchEvent 方法中返回了 true。所以所有其他事件都在 onTouchEvent 中发送,而不是在 onInterceptTouchEvent 中发送:

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)

关于android - onInterceptTouchEvent 的 ACTION_UP 和 ACTION_MOVE 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21288081/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com