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

android:descendantFocusability用法简析

时间:2014-09-09 21:36:39      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   http   color   io   使用   ar   strong   

开发中很常见的一个问题,项目中的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.

bubuko.com,布布扣

 

该属性是当一个为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用法简析

标签:des   android   style   http   color   io   使用   ar   strong   

原文地址:http://blog.csdn.net/tw19811220/article/details/39160895

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