ListItem布局中的checkbox可能导致整行选择失败

编者:李国帅

qq:9611153 微信lgs9611153

时间:2020-04-10

背景原因:

在android应用程序开发中,列表是常见的界面元素,然而,经常会遇到列表的子项无法选中的现象。

 

列表的选项即应该灵敏,也要避免重复点击造成的错误。

这个要分情况:

         如果是点击之后需要和后台互动,或者需要进行跳转,那么就需要避免多次快速点击。

         如果仅仅时应用程序内部数据或者界面操作,那么就需要及时反应,避免影响用户操作,给人反应迟缓的印象。

 

下面遇到的就是仅仅本地操作时,点击事件不能马上反应的问题。遇到了以下这个问题。

解决方法:

         常会遇到这种情况,RecyclerView或者ListView中的子项list_item_xxx.xml,触发点击整行事件时,当点击checkbox区域可能无法触发点击整行事件。

         应该是checkbox点击事件阻挡了点击整行事件,其实edittext,button,radiobox这些控件也是可能影响到整行点击事件的。

         要解决,只需要把CheckBox更改为ImageView,自己控制view的显示样式即可,就不会影响到点击事件了。

         下面就是解决的实例。

原本代码:

item处理部分

 

list_item.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="@color/C_FFFFFF"

        android:orientation="vertical">


        <LinearLayout

            android:id="@+id/ll_item_list_main"

            android:layout_width="match_parent"

            android:layout_height="@dimen/dp_100"

            android:layout_marginLeft="@dimen/dp15"

            android:background="@color/C_FFFFFF"

            android:gravity="center_vertical"

            android:orientation="horizontal">


            <CheckBox

                android:id="@+id/cb_item_list"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginLeft="10dp"

                android:button="@drawable/checkbox_selector"

                android:enabled="false"

                android:focusable="false" />

更改为:

item处理部分

 

 

list_item.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android">


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="@color/C_FFFFFF"

        android:orientation="vertical">


        <LinearLayout

            android:id="@+id/ll_item_list_main"

            android:layout_width="match_parent"

            android:layout_height="@dimen/dp_100"

            android:layout_marginLeft="@dimen/dp15"

            android:background="@color/C_FFFFFF"

            android:gravity="center_vertical"

            android:orientation="horizontal">


            <ImageView

                android:id="@+id/iv_item_list"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginLeft="10dp"

                android:src="@drawable/check_box_nor"

                android:focusable="false" />

 

点击回调时手动处理显示状态:

 

 

如此便不会出现无法选中列表项的情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微澜-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值