关于ListView中嵌套CheckBox时,两者点击事件的响应问题

关于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>

以上解决方案,本人亲测有效,若有不足,欢迎指正!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值