日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android:descendantFocusability用法简析

發(fā)布時間:2024/4/15 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android:descendantFocusability用法简析 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

開發(fā)中很常見的一個問題,項目中的listview不僅僅是簡單的文字,常常需要自己定義listview,自己的Adapter去繼承BaseAdapter,在adapter中按照需求進行編寫,問題就出現(xiàn)了,可能會發(fā)生點擊每一個item的時候沒有反應(yīng),無法獲取的焦點。原因多半是由于在你自己定義的Item中存在諸如ImageButton,Button,CheckBox等子控件(也可以說是Button或者Checkable的子類控件),此時這些子控件會將焦點獲取到,所以常常當點擊item時變化的是子控件,item本身的點擊沒有響應(yīng)。

??? 這時候就可以使用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和其子控件兩者之間的關(guān)系。

屬性的值有三種:

??????? beforeDescendants:viewgroup會優(yōu)先其子類控件而獲取到焦點

??????? afterDescendants:viewgroup只有當其子類控件不需要獲取焦點時才獲取焦點

??????? blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點

?

通常我們用到的是第三種,即在Item布局的根布局加上android:descendantFocusability=”blocksDescendants”的屬性就好了,至此listview點擊的靈異事件告一段落。心得:遇到不會不懂的地方除了網(wǎng)上查詢資料之外,也可以多多去嘗試每種屬性的作用,多閱讀官方文檔(我始終覺得還是讀原文的比翻譯的理解的會更好)。

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


? ? ? ? <!-- 顯示組名和聯(lián)系人人數(shù) -->


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


總結(jié)

以上是生活随笔為你收集整理的android:descendantFocusability用法简析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。