我在父布局上设置了一个View.OnTouchListener,但似乎并没有起作用。
下面是UI的XML代码:
尽管这样也没有起作用。 为什么父布局没有触摸事件?我应该如何让它工作?
下面是UI的XML代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical"
android:id="@+id/mainActivityLinearLayout"
android:background="@drawable/listviewborder"
tools:context=".MainActivity" >
<View
android:id="@+id/lineView"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/gray" />
<ListView
android:id="@+id/gameListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:dividerHeight="2dp" />
</LinearLayout>
这里是设置在父布局上的OnTouchListener:
mainActivityView = this.findViewById(R.id.mainActivityLinearLayout);
mainActivityView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("Touch test activity");
return true;
}
});
onTouch()
方法从未被调用,字符串也未打印出来。
我已经为ListView实现了OnItemClick,现在想要检测整个布局的触摸事件。
我也尝试过:
gameListView = (ListView) findViewById(R.id.gameListView);
gameListView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("Touch test list");
return true;
}
});
尽管这样也没有起作用。 为什么父布局没有触摸事件?我应该如何让它工作?