Returning false in OnTouch on ImageView but still event is getting consumed(在 ImageView 上的 OnTouch 中返回 false,但仍然消耗事件)
问题描述
我正在使用 ImageView.onTouch().我为 ACTION_MOVE 返回 false,但仍然消耗了 onTouch() 事件.
I am using ImageView.onTouch().
I am returning false for ACTION_MOVE but still the onTouch() event is consumed.
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent ev) {
System.out.println("jan25 iv onTouch");
if (ev.getActionMasked()==MotionEvent.ACTION_DOWN) {
System.out.println("jan25 iv ACTION_DOWN");
return true;
}
if (ev.getActionMasked()==MotionEvent.ACTION_MOVE){
System.out.println("jan25 iv ACTION_MOVE");
return false;
}
if (ev.getActionMasked()==MotionEvent.ACTION_UP){
System.out.println("jan25 iv ACTION_UP");
return true;
}
System.out.println("jan25 iv false");
return false;
}
});
布局:
<LinearLayout
android:id="@+id/ll_magic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/red"
android:orientation="vertical" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abcd"
/>
</LinearLayout>
有什么建议
推荐答案
好像view的onTouchEvent()回调返回false只有在接收时才有效ACTION_DOWN 的第一个动作事件.在非消费视图上设置 OnTouchListener 具有相同的语义.这似乎是 ViewGroup 中的 dispatchTouchEvent() 实现中的错误.不过,它似乎不太可能在这么长时间后得到修复,因为这会破坏错误地依赖于这种行为的实现.
It seems that returning false from the onTouchEvent() callback of a view is only effective when receiving the first motion event with ACTION_DOWN. Setting an OnTouchListener on a non-consuming view has the same semantics. This seems to be a bug in the dispatchTouchEvent() implementation in ViewGroup. It doesn't seem likely that it will be fixed after such a long time though, as that would break implementations that incorrectly depend on this behavior.
作为一种解决方法,您可以设置一个标志来指示触摸事件的处理,并在处理之前检查它.此标志应在收到 ACTION_UP 或 ACTION_CANCEL 运动事件后重置.
As a workaround, you can set a flag indicating the handling of touch events, and check that before handling. This flag should be reset upon receiving the ACTION_UP or ACTION_CANCEL motion events.
这篇关于在 ImageView 上的 OnTouch 中返回 false,但仍然消耗事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 ImageView 上的 OnTouch 中返回 false,但仍然消耗事件
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
