码迷,mamicode.com
首页 > 移动开发 > 详细

Android疑惑篇------------含有button按钮的ListView中,列表项无法获取焦点的问题

时间:2015-07-20 21:26:25      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

问题描述:

有过在ListView的列表项中添加按钮的朋友对这个问题一定不会陌生,我们的Demo在运行时,会出现这样的情况----------可以获取到列表项中的按钮点击事件,但是当我们想要获取整个列表项的点击事件时,就会发现此时点击列表项是没有任何反应的.为什么呢?

原因就是当在ListView中加入Button这类的有 “点击” 事件的widget时,ListView的itemclick事件将会被其它widget的click事件屏蔽,从而无法触发.

 

我们先来看一下列表项的XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/border_corner"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center"
    android:orientation="horizontal" >

    <!--
        beforeDescendants:viewgroup会优先其子类控件而获取到焦点
           afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
        blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
    -->

    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:contentDescription="联系人头像"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvContactName"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:text=" 小明"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tvContactNumber"
            android:layout_width="wrap_content"
            android:layout_height="15dp"
            android:text="11111111111" />
    </LinearLayout>

<!--         
    android:focusable="false"  设定为该控件不可获取焦点
 -->

    <Button
        android:id="@+id/btnCall"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:background="@drawable/bg_btn_call"
        android:contentDescription="打电话"
        android:focusable="false" />

    <Button
        android:id="@+id/btnSendMessage"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/bg_btn_message"
        android:contentDescription="发短信"
        android:focusable="false" />

</LinearLayout>

 

有没有发现什么特别的东西呢?

 

解决方法一:

技术分享

看到方框内的内容了吗?

android:descendantFocusability="blocksDescendants"

descendantFocusability属性的作用是在为一个View获取焦点时,设定ViewGroup和其子控件之间的关系.其属性值如下:

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

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

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

在这三种属性中,最常用到的就是第3种,即在Item布局的根布局上添加该属性,至此,就可以正确的获取到列表项了.

 

解决方法二:

在文章的开头,我们提到了当在ListView加入了Button这类的有 “点击” 事件的widget时,ListView的itemclick事件将会被其它widget的click事件屏蔽,从而无法触发.

那么解决的方法就来了:

技术分享

如上图:

android:focusable="false"

当Button按钮的focusable的属性被设定为false时,该控件就不可再获取焦点了,由此,自然也就无法屏蔽掉ListView的itemClick事件了.

 

个人认为,方法2与方法1是有着一定的相似性的,都是在使ListView中的Item获取焦点,从而触发ItemClick事件.如果有什么不同的意见,欢迎留言与小编探讨.(*^__^*)

 

资料链接:

链接1:    http://blog.csdn.net/gyflyx/article/details/6567701

链接2:  http://mobile.51cto.com/android-265197_3.htm

Android疑惑篇------------含有button按钮的ListView中,列表项无法获取焦点的问题

标签:

原文地址:http://www.cnblogs.com/toast/p/4662571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!