ListView.setOnItemClickListener 不觸發,常見原因:項目佈局(row3.xml)中的控件設置了 android:focusable="true"、clickable="true" 或自定義 OnClickListener,攔截了觸摸事件導致 ListView 無法檢測項目點擊。
android:focusable="true"、android:focusableInTouchMode="true"、android:clickable="true",去掉或設置為 false。
<LinearLayout ... android:focusable="false" android:clickable="false"> ... </LinearLayout>
setOnClickListener(示例代碼中無,但需確認)。
android:focusable="false"、android:focusableInTouchMode="false"(默認即可)。
getViewTypeCount > 1),確保所有類型都不攔截觸摸。
listView.setOnItemClickListener((parent, view, position, id) -> {
Log.d("ListView", "Item clicked: " + position);
Person person = (Person) parent.getItemAtPosition(position);
Log.d("ListView", "Name: " + person.name); // 直接獲取數據對象
});
getItemAtPosition(position) 返回的是 Adapter 對應位置的數據對象(示例中 Person),直接強轉取字段,勿用 toString()。
清理項目佈局焦點/點擊屬性,ListView 項目點擊即可恢復正常。