ListView的item中加入CheckBox後 致使ListView對OnItemClick事件沒法響應 緣由是由於CheckBox的事件響應優先級高於List Item,因此屏蔽了ListItem的單擊事件。 網上好多回答都是將checkBox的click事件屏蔽掉去,以下所示:java
<!-- lang: xml --> <CheckBox android:id="@+id/cb_process_manager_state" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="10dip" android:onClick="false" android:clickable="false"/>
在上面代碼中多添加了兩個屬性 android:onClick="false" android:clickable="false"這樣就能夠讓checkBox事件不起做用了。 可是通過測試發現,這樣仍是不行。還須要作一下改變: 在item的根元素中增長這個屬性:android:descendantFocusability=」blocksDescendants」,這樣就能夠觸發OnItemClick和OnItemLongClickListener事件了。 附:android
<!-- lang: java --> android:descendantFocusability
Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.測試
Must be one of the following constant values.code
該屬性是當一個爲view獲取焦點時,定義viewGroup和其子控件二者之間的關係。xml
屬性的值有三種:事件
beforeDescendants:viewgroup會優先其子類控件而獲取到焦點 afterDescendants:viewgroup只有當其子類控件不須要獲取焦點時才獲取焦點 blocksDescendants:viewgroup會覆蓋子類控件而直接得到焦點