Android 中ListView點(diǎn)擊Item無響應(yīng)問題的解決辦法
如果listitem里面包括button或者checkbox等控件,默認(rèn)情況下listitem會失去焦點(diǎn),導(dǎo)致無法響應(yīng)item的事件,最常用的解決辦法是在listitem的布局文件中設(shè)置descendantFocusability屬性。
item的布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:paddingLeft="5dp" android:paddingRight="5dp" android:descendantFocusability="blocksDescendants"><!--添加這個屬性--> <CheckBox android:id="@+id/history_item_checkbt" android:layout_height="30dp" android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:checked="false" > </CheckBox> <ImageView android:id="@+id/history_item_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/history_item_checkbt" android:background="@drawable/item_icon"> </ImageView> <Button android:id="@+id/history_item_edit_bt" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="編輯" android:textColor="#ffffff" android:textSize="14sp" android:background="@drawable/button_bg"> </Button> <TextView android:id="@+id/history_item_time_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:textColor="#565C5D" android:textSize="14sp" android:text="10-01 10:20" android:layout_marginRight="5dp" android:layout_toLeftOf="@id/history_item_edit_bt"> </TextView> <TextView android:id="@+id/history_item_title_tv" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_centerVertical="true" android:textColor="#565C5D" android:textSize="14sp" android:text="xxxxxxxxXXXXXXXXXXXXXXXX" android:ellipsize="end" android:maxLines="1" android:layout_toRightOf="@id/history_item_image" android:layout_toLeftOf="@id/history_item_time_tv" android:layout_marginLeft="3dp"> </TextView> </RelativeLayout>
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.
該屬性是當(dāng)一個為view獲取焦點(diǎn)時,定義viewGroup和其子控件兩者之間的關(guān)系。
屬性的值有三種:
beforeDescendants:viewgroup會優(yōu)先其子類控件而獲取到焦點(diǎn)
afterDescendants:viewgroup只有當(dāng)其子類控件不需要獲取焦點(diǎn)時才獲取焦點(diǎn)
blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點(diǎn)
我們使用的是第三個。
相關(guān)文章
Android數(shù)據(jù)持久化之File機(jī)制分析
這篇文章主要介紹了Android數(shù)據(jù)持久化之File機(jī)制,較為詳細(xì)的分析了File機(jī)制的原理及Android使用File實(shí)現(xiàn)數(shù)據(jù)持久化的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05Android控件AppWidgetProvider使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android控件AppWidgetProvider的使用方法詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android判斷App前臺運(yùn)行還是后臺運(yùn)行(運(yùn)行狀態(tài))
這篇文章主要介紹了Android判斷App前臺運(yùn)行還是后臺運(yùn)行的相關(guān)資料,需要的朋友可以參考下2016-04-04Android形狀圖形與狀態(tài)列表圖形及九宮格圖片超詳細(xì)講解
這篇文章主要介紹了Android形狀圖形與狀態(tài)列表圖形及九宮格圖片的應(yīng)用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09Android異步消息處理機(jī)制實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Android異步消息處理機(jī)制實(shí)現(xiàn)原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09Android實(shí)現(xiàn)單頁顯示3個Item的ViewPager炫酷切換效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)單頁顯示3個Item的ViewPager炫酷切換效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10一文詳解如何在Flutter中使用導(dǎo)航Navigator
一個APP如果沒有頁面跳轉(zhuǎn)那么是沒有靈魂的,頁面跳轉(zhuǎn)的一個常用說法就是Navigator。那么在flutter中如何使用Navigator呢?本文就來和大家詳細(xì)聊聊2023-02-02