我在父佈局上設置了View.OnTouchListener,但它看起來不起作用。View.OnTouchListener()在父佈局上不起作用
下面是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;
}
});
雖然這也不能工作。
爲什麼父級佈局沒有獲得觸摸事件?我怎樣才能使它工作?
是否存在LinearLayout的任何部分?如果一個ListView在它上面,並且處理的點擊,你的LinearLayout將不會得到它。 – MikeHelland
從您的代碼中,我發現佈局中填充了2個灰度,然後是所有列表視圖,這意味着沒有可實際觸及的父佈局空間。你可能想要添加填充到你的父母,以便你可以捕捉觸摸事件 –
@MazeHatter我知道,是的,listview是在整個佈局的頂部。只是想知道是否有這樣的設計,以便LinearLayout即使在它下面也能捕獲觸摸事件。 –