LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<ImageView
android:id="@+id/rowIV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
>
</ImageView>
<TextView
android:id="@+id/rowTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
>
</TextView>
<CheckBox
android:id="@+id/rowCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
</LinearLayout>
如果你的自定義ListViewItem中有Button或者Checkable的子類控件的話,那么默認focus是交給了子控件,而ListView的Item能被選中的基礎是它能獲取Focus,也就是說我們可以通過將ListView中Item中包含的所有控件的focusable屬性設置為false,這樣的話ListView的Item自動獲得了Focus的權限,也就可以被選中了
我們可以通過對Item Layout的根控件設置其android:descendantFocusability=”blocksDescendants”即可,這樣Item Layout就屏蔽了所有子控件獲取Focus的權限,不需要針對Item Layout中的每一個控件重新設置focusable屬性了,如此就可以順利的響應onItemClickListener中的onItemClick()方法了。
也可以將每個子控件寫成 android:focusable="false"的,讓控件失去焦點。
