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

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

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

res/layout/item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
          <TextView
                android:id="@+id/tauthor"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="名字"/>
       <ImageButton
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginTop="15dp"
            android:layout_marginRight="25dp"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher"/>      
</LinearLayout>

以上是我的item布局文件

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

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

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

最后整理结论得:

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

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

两者缺一不可





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值