开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了,可能会发生点击每一个item的时候没有反应,无法获取的焦点。原因多半是由于在你自己定义的Item中存在诸如ImageButton,Button,CheckBox等子控件(也可以说是Button或者Checkable的子类控件),此时这些子控件会将焦点获取到,所以常常当点击item时变化的是子控件,item本身的点击没有响应。

这时候就可以使用descendantFocusability来解决啦,API描述如下:

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会覆盖子类控件而直接获得焦点

通常我们用到的是第三种,即在Item布局的根布局加上android:descendantFocusability=”blocksDescendants”的属性就好了,至此listview点击的灵异事件告一段落。心得:遇到不会不懂的地方除了网上查询资料之外,也可以多多去尝试每种属性的作用,多阅读官方文档(我始终觉得还是读原文的比翻译的理解的会更好)。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/background" >

<RelativeLayout
        android:id="@+id/rel_group"
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:layout_toLeftOf="@+id/linear_view"
    android:background="@drawable/list_selector"
    android:descendantFocusability="blocksDescendants"
       >

<ImageView
            android:id="@+id/iamge_group_photo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dip"
            android:background="@drawable/btn_group_change_normal"
            android:contentDescription="@string/app_name" />

<!-- 显示组名和联系人人数 -->

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dip"
            android:layout_toRightOf="@+id/iamge_group_photo"
            android:orientation="vertical" >

<TextView
                android:id="@+id/txt_group_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:maxEms="8" 
               android:singleLine="true"
                android:ellipsize="end"
                android:textSize="17sp" />

<TextView
                android:id="@+id/txt_contact_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dip"
                android:textSize="14sp" />
        </LinearLayout>
    </RelativeLayout>

<LinearLayout
        android:id="@+id/linear_view"
        android:layout_width="0.5dip"
        android:layout_height="50dip"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/rel_delete"
        android:background="@drawable/list_item_divide_line" />

<!-- 删除分组按钮 -->

<RelativeLayout
        android:id="@+id/rel_delete"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:layout_alignParentRight="true"
        android:background="@drawable/list_selector"
       android:descendantFocusability="blocksDescendants" >

<ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/delete_groupmember_icon_selector"
            android:contentDescription="@string/app_name" />
    </RelativeLayout>

</RelativeLayout>

android:descendantFocusability用法简析相关推荐

  1. 坑爹的属性,android:descendantFocusability用法简析

    开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承 BaseAdapter,在adapter中按照需求进行编写,问题就出现 ...

  2. android焦点优先级,Viewgroup焦点获取优先级android:descendantFocusability用法简析

    开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了 ...

  3. android之descendantFocusability用法简析

    2019独角兽企业重金招聘Python工程师标准>>> listView的Item被抢焦点,这是开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义l ...

  4. Python中的基本函数及其常用用法简析

    分享Python中的基本函数及其常用用法简析,首先关于函数的解释函数是为了达到某种目的而采取的行为,函数是可重复使用的,用来实现某个单一功能或者功能片段的代码块,简单来说就是由一系列的程序语句组成的程 ...

  5. fuser 用法简析

    fuser 用法简析 fuser [功能]  fuser 可以显示出当前哪个程序在使用磁盘上的某个文件.挂载点.甚至网络端口,并给出程序进程的详细信息. [描述]  fuser显示使用指定文件或者文件 ...

  6. Android 启动过程简析

    首先我们先来看android构架图: android系统是构建在linux系统上面的. 所以android设备启动经历3个过程. Boot Loader,Linux Kernel & Andr ...

  7. Android Telephony框架结构简析

    Android Telephony涉及的框架结构如图1所示. 图1  Android Telephony框架结构 通过图1可以发现Android Telephony框架结构的一些规律,具体如下. An ...

  8. Qualcomm Android camera 架构简析及如何debug

    一. Camera模组(CCM)介绍: CCM一般包含四大件: 镜头(lens).传感器(sensor).软板(FPC).图像处理芯片(DSP):     Camera的成像原理可以简单概括如下: 1 ...

  9. PrintWriter用法简析

    public class PrintWriterextends Writer 向文本输出流打印对象的格式化表示形式.此类实现在 PrintStream 中的所有 print 方法.它不包含用于写入原始 ...

最新文章

  1. python基础代码事例-Python基础总结成千行代码,让Python入门更简单!
  2. vc编写供vb使用的dll文档
  3. java-并发-并发容器(3)
  4. 一、目前在ASP.NET中页面传值共有这么几种方式:
  5. 如何连接两个窗口JAVA_java-如何连接两个ArrayLists?
  6. 成长 | 《大厂晋升指南》学习总结(中)
  7. JDK 14的征兆开始出现
  8. python基础入门(3)之字符串
  9. 远程计算机怎么安装软件安装,轻松一步把电脑上的软件远程安装到电视上!
  10. Java并发编程:synchronized
  11. python 重命名文件出现乱码_python处理文件名乱码
  12. linux刷新本地dns命令_如何刷新本地DNS缓存
  13. LVM -逻辑卷管理
  14. methodinvocationexception: property ‘driverclassname‘ threw exception; nested exception is java.lang
  15. 常识——server-u显示无法访问网页
  16. Cascading Convolutional Color Constancy
  17. 安卓手机上有哪些好用的小说阅读器?
  18. 以下是一段歌词,请从这段歌词中统计出“月亮”一词在歌词中出现的次数。
  19. 网站每天更新几十篇上百篇文章是怎么做到的?
  20. 移动光猫调整桥接模式

热门文章

  1. 关于mysql触发器和存储过程的理解
  2. JavaScript中的坐标
  3. UVa 10375 - Choose and divide(唯一分解定理)
  4. [Leetcode] Reverse Integer
  5. OpenCV中Mat的属性
  6. Js String转Int(Number与parseInt的区别)
  7. 王家林 云计算分布式大数据Hadoop实战高手之路第七讲Hadoop图文训练课程:通过HDFS的心跳来测试replication具体的工作机制和流程...
  8. Cucumber入门之_argument
  9. 不迈出第一步,不去尝试,你永远不知道能否成功
  10. 自动化设计模式Page Object