您的位置:首页 > 其它

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

2016-03-11 12:34 561 查看

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

2016-03-11 12:34
179人阅读 评论(0)
收藏
举报


分类:

android(14)


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

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

res/layout/item.xml

[html]
view plain
copy







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

两者缺一不可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: