通常我们在处理控件(view)touch事件的时候,会发生获取不到ACTION_DOWN事件的情况。
例如,运行以下代码后,在屏幕上无论如何点击、长按、滑动这个控件(view),logcat上只能看到 ACTION_DOWN 的输出
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("","ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.e("","ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.e("","ACTION_UP");
break;
}
return false;
}
});
在目标控件(view)的布局中,添加属性:android:clickable="true"
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" />