在 Android 文档中,它说ViewGroup的OnInterceptTouchEvent方法:
允许您在事件发送给您的孩子时观看它们
这对我来说有点暗示,它只会观看它的孩子的事件,不包括它自己的事件。不应该是这样吗?
我做了一个快速测试,无论我是点击子视图(绿色方块)还是在 ViewGroup 本身的外部,该方法都会触发:
自定义视图:
public class TestViewGroup extends FrameLayout {
/* Constructors
and other bla bla... */
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.d("TestViewGroup", "TheTruth: " + ev.toString());
return true;
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<com.example.someapp.main.TestViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="bottom"
android:background="#151" />
</com.example.someapp.main.TestViewGroup>
为什么?
最终我会在绿色方块的地方显示一张地图,我只想听那些从地图区域之外开始的触摸屏事件。