0
点赞
收藏
分享

微信扫一扫

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


编者:李国帅


背景原因:

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

 

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

这个要分情况:

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

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

 

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

解决方法:

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

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

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

         下面就是解决的实例。

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

原本代码:

item处理部分

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

 

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处理部分

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

 

 

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

 

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

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

 

 

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

举报

相关推荐

0 条评论