ListItem布局中的checkbox阻碍整行选择的处理办法

编者:李国帅

qq:9611153 微信lgs9611153

时间:2020/4/10 

背景:

在android应用程序开发中,列表是常见的界面元素,然而,经常会遇到列表的子项无法选中的现象。或者表现为不是总是能够触发事件,点击几下才反应。

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

解决方法:

遇到这个问题解决方法,应该是checkbox点击事件阻挡了点击整行事件。

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

 

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

item处理部分:

原本代码

        thisBinding.cbItemListDoctor.setChecked(info.getSelected());//更改状态
        itemView.setOnClickListener((v -> onClickListener.clickItem(itemView, info, 0)));//激发整行点击

更改为

       if (info.getSelected())
            thisBinding.ivItemListDoctor.setImageResource(R.drawable.check_box_sel);//更改状态
        else
            thisBinding.ivItemListDoctor.setImageResource(R.drawable.check_box_nor);

        itemView.setOnClickListener((v -> onClickListener.clickItem(itemView, info, 0)));//点击整行

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" />

更改为

<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" />

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

 

    public boolean clickItem(Object obj, int type) {
        PageList item = (PageList) obj;
        Boolean selected = !item.getSelected();//修改数据
        item.setSelected(selected);

        /*ArrayList<ListItem> lstInfo = adapter.getData(); //如若单选,还需要把其他的数据项设置为false
        for (int i = 0; i < lstInfo.size(); i++) {
            ListItem item1 = lstInfo.get(i);
            if (item1.getSelected()) {
                item1.setSelected(false);
                break;
            }
        }*/
        adapter.notifyDataSetChanged();//更新列表

        return true;
    }

 

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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

微澜-

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

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

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

打赏作者

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

抵扣说明:

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

余额充值