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会覆盖子类控件而直接得到焦点