在listview的item中包含有textview和checkBox。我们既想获取listitem的点击事件,又想获取listitem中textview的点击事件和listitem中checkBox的点击事件。这篇文章主要介绍了listView的item中有checkbox,导致setOnItemClick失效的原因及解决办法,需要的朋友可以参考下:
一:item的根布局设置
Android:clickable="true"
,之后导致item点击事件失效,对根布局设置android:descendantFocusability="blocksDescendants
",以及对checkbox设置android:focusable="false"
都不会起作用,所以item根布局不要设置android:clickable="true"
二:item根布局设置android:descendantFocusability="blocksDescendants
",即可
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
三:checkbox设置android:focusable="false"
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_item_gray_selector" android:descendantFocusability="blocksDescendants" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="17dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="17dp"> <com.saj.esolar.widget.SmoothCheckBox android:id="@+id/cb_card_item" android:layout_width="20dp" android:layout_height="20dp" android:layout_gravity="center_vertical|right" android:layout_margin="5dp" android:layout_weight="1" android:focusable="false"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_toLeftOf="@+id/tv_remaining_days" android:layout_toRightOf="@+id/cb_card_item" android:orientation="vertical"> <TextView android:id="@+id/tv_card_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:lines="1" android:textColor="@color/color_text_gray3" android:textSize="14sp" tools:text="11111111111111111111111111111111111111111"/> <TextView android:id="@+id/tv_date_due" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:lines="1" android:textColor="@color/color_text_gray2" android:textSize="14sp" tools:text="2222222222222222222222"/> </LinearLayout> <TextView android:id="@+id/tv_remaining_days" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:layout_toLeftOf="@+id/iv_jump" android:layout_toStartOf="@+id/iv_jump" android:textColor="@color/color_text_gray3" android:textSize="14sp" tools:text="剩余15天"/> <ImageView android:id="@+id/iv_jump" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@drawable/jump_gray"/> </RelativeLayout> </RelativeLayout>
===========================================================================
===========================================================================
亲测有效。