Android ListView的item点击无响应的解决方法

   如果listitem里面包括button或者checkbox等控件,默认情况下listitem会失去焦点,导致无法响应item的事件,最常用的解决办法

是在listitem的布局文件中设置descendantFocusability属性。

item的布局文件:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="wrap_content"  
  5.   android:layout_height="wrap_content"  
  6.   android:paddingTop="10dp"  
  7.   android:paddingBottom="10dp"  
  8.   android:paddingLeft="5dp"  
  9.   android:paddingRight="5dp"  
  10.   android:descendantFocusability="blocksDescendants"><!--添加这个属性-->  
  11.   <CheckBox  
  12.    android:id="@+id/history_item_checkbt"  
  13.    android:layout_height="30dp"  
  14.    android:layout_width="wrap_content"  
  15.    android:layout_centerVertical="true"  
  16.    android:layout_alignParentLeft="true"  
  17.    android:checked="false"  
  18.    >  
  19.   </CheckBox>  
  20.   
  21.   <ImageView  
  22.    android:id="@+id/history_item_image"  
  23.    android:layout_width="wrap_content"  
  24.    android:layout_height="wrap_content"  
  25.    android:layout_centerVertical="true"  
  26.    android:layout_toRightOf="@id/history_item_checkbt"  
  27.    android:background="@drawable/item_icon">  
  28.   </ImageView>  
  29.   
  30.     
  31.   <Button  
  32.    android:id="@+id/history_item_edit_bt"  
  33.    android:layout_alignParentRight="true"  
  34.    android:layout_width="wrap_content"  
  35.    android:layout_height="wrap_content"  
  36.    android:layout_centerVertical="true"  
  37.    android:text="编辑"  
  38.    android:textColor="#ffffff"  
  39.    android:textSize="14sp"  
  40.    android:background="@drawable/button_bg">  
  41.   </Button>  
  42.   
  43.   <TextView  
  44.    android:id="@+id/history_item_time_tv"  
  45.    android:layout_width="wrap_content"  
  46.    android:layout_height="wrap_content"  
  47.    android:layout_centerVertical="true"  
  48.    android:textColor="#565C5D"  
  49.    android:textSize="14sp"  
  50.    android:text="10-01 10:20"  
  51.    android:layout_marginRight="5dp"  
  52.    android:layout_toLeftOf="@id/history_item_edit_bt">  
  53.   </TextView>  
  54.   
  55.   <TextView  
  56.    android:id="@+id/history_item_title_tv"  
  57.    android:layout_height="wrap_content"  
  58.    android:layout_width="fill_parent"  
  59.    android:layout_centerVertical="true"  
  60.    android:textColor="#565C5D"  
  61.    android:textSize="14sp"  
  62.    android:text="xxxxxxxxXXXXXXXXXXXXXXXX"  
  63.    android:ellipsize="end"  
  64.    android:maxLines="1"  
  65.    android:layout_toRightOf="@id/history_item_image"  
  66.    android:layout_toLeftOf="@id/history_item_time_tv"  
  67.    android:layout_marginLeft="3dp">  
  68.   </TextView>  
  69. </RelativeLayout>  

android:descendantFocusability

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

Must be one of the following constant values.

 

该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。

属性的值有三种:

        beforeDescendants:viewgroup会优先其子类控件而获取到焦点

        afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点

        blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

  我们使用的是第三个。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值