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"的,让控件失去焦点。