ListView内item与Button共存的点击监听问题

         ListView内item与Button共存的点击监听问题

179人阅读 评论(0) 收藏 举报
分类:

        最近写ListView,在其中的item中加入了ImageView和Button。发现ListView的点击事件突然不可以了,这是因为加入其它widget后,ListView的itemclick事件将无法触发,被其它widget的click事件屏蔽。

首先说明我的ListView的item布局中包含:TextView、Button

res/layout/item.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.           <TextView  
  8.                 android:id="@+id/tauthor"  
  9.                 android:layout_width="wrap_content"  
  10.                 android:layout_height="wrap_content"  
  11.                 android:layout_marginTop="5dp"  
  12.                 android:text="名字"/>  
  13.        <ImageButton  
  14.             android:layout_width="40dp"  
  15.             android:layout_height="40dp"  
  16.             android:layout_marginTop="15dp"  
  17.             android:layout_marginRight="25dp"  
  18.             android:layout_weight="1"  
  19.             android:src="@drawable/ic_launcher"/>        
  20. </LinearLayout>  

以上是我的item布局文件

        一开始我按照以前的经验,在ImageButton中加入了android:focusable="false" ,测试还是不行。感觉挺郁闷,

后来我通过查找资料,在最外层的布局中加入了 android:descendantFocusability="blocksDescendants",果然就可以了

另外我测试只单单在最外层加上:android:descendantFocusability="blocksDescendants"。也是不行的。

最后整理结论得:

1、在最外层布局中加入:android:descendantFocusability="blocksDescendants"

2、在Button、ImageButton,或者有相同特点的控件中加入:android:focusable="false"

两者缺一不可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值