关于ListView中嵌套CheckBox时,两者点击事件的响应问题
由于近期公司项目要新增全选/取消全选功能,而项目之前使用的是ListView+CheckBox方案,并且同时保留了ListView的item的点击事件和CheckBox的点击事件,但在完成全选/取消全选功能之后,点击CheckBox会导致全选框显示的混乱,由此需要屏蔽CheckBox的点击事件。
然而,在网上搜索之后发现并不能实现(可能是我才疏学浅),故此自己摸索实现了此功能,现将解决方案记录于此。
1.两者都响应点击事件
在item最外层布局加上属性:android:descendantFocusability=”blocksDescendants”
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="50dp"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/tv_name"
style="@style/tv_style"
android:text="@string/full_name"/>
<TextView
android:id="@+id/tv_gender"
style="@style/tv_style"
android:text="@string/gender"/>
<TextView
android:id="@+id/tv_id_number"
style="@style/tv_style"
android:text="@string/id_no"/>
<TextView
android:id="@+id/tv_upload_state"
style="@style/tv_style"
android:text="@string/upload_state"/>
<TextView
android:id="@+id/tv_update_time"
style="@style/tv_style"
android:text="@string/update_time"/>
<CheckBox
android:id="@+id/checkbox_operate_data"
style="@style/tv_style"
android:layout_weight="1"
android:button="@drawable/bg_check_box"
android:visibility="gone"/>
</LinearLayout>
2.只要CheckBox响应点击事件
给CheckBox设置属性:android:clickable=”false”
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/tv_name"
style="@style/tv_style"
android:text="@string/full_name"/>
<TextView
android:id="@+id/tv_gender"
style="@style/tv_style"
android:text="@string/gender"/>
<TextView
android:id="@+id/tv_id_number"
style="@style/tv_style"
android:text="@string/id_no"/>
<TextView
android:id="@+id/tv_upload_state"
style="@style/tv_style"
android:text="@string/upload_state"/>
<TextView
android:id="@+id/tv_update_time"
style="@style/tv_style"
android:text="@string/update_time"/>
<CheckBox
android:id="@+id/checkbox_operate_data"
style="@style/tv_style"
android:layout_weight="1"
android:button="@drawable/bg_check_box"
android:clickable="false"
android:visibility="gone"/>
</LinearLayout>
3.只要ListView的item响应点击事件
在item最外层布局加上属性:android:descendantFocusability=”blocksDescendants”,
并且给CheckBox设置属性:android:clickable=”false”; android:enabled=”false”。
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="50dp"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/tv_name"
style="@style/tv_style"
android:text="@string/full_name"/>
<TextView
android:id="@+id/tv_gender"
style="@style/tv_style"
android:text="@string/gender"/>
<TextView
android:id="@+id/tv_id_number"
style="@style/tv_style"
android:text="@string/id_no"/>
<TextView
android:id="@+id/tv_upload_state"
style="@style/tv_style"
android:text="@string/upload_state"/>
<TextView
android:id="@+id/tv_update_time"
style="@style/tv_style"
android:text="@string/update_time"/>
<CheckBox
android:id="@+id/checkbox_operate_data"
style="@style/tv_style"
android:layout_weight="1"
android:button="@drawable/bg_check_box"
android:clickable="false"
android:enabled="false"
android:visibility="gone"/>
</LinearLayout>
在这种情况下,如果给CheckBox设置了样式,需要在selector中将android:state_enabled=”true”属性删掉,不然CheckBox在选中时不显示选中时的图标。
selector文件如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_check_on_holo_dark" android:state_checked="true"/>
<item android:drawable="@drawable/btn_check_off_holo_light"/>
</selector>