Android 中ListView點擊Item無響應問題的解決辦法
如果listitem里面包括button或者checkbox等控件,默認情況下listitem會失去焦點,導致無法響應item的事件,最常用的解決辦法是在listitem的布局文件中設置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.
該屬性是當一個為view獲取焦點時,定義viewGroup和其子控件兩者之間的關系。
屬性的值有三種:
beforeDescendants:viewgroup會優(yōu)先其子類控件而獲取到焦點
afterDescendants:viewgroup只有當其子類控件不需要獲取焦點時才獲取焦點
blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點
我們使用的是第三個。
相關文章
Android數(shù)據(jù)持久化之File機制分析
這篇文章主要介紹了Android數(shù)據(jù)持久化之File機制,較為詳細的分析了File機制的原理及Android使用File實現(xiàn)數(shù)據(jù)持久化的相關操作技巧,需要的朋友可以參考下2017-05-05Android控件AppWidgetProvider使用方法詳解
這篇文章主要為大家詳細介紹了Android控件AppWidgetProvider的使用方法詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android判斷App前臺運行還是后臺運行(運行狀態(tài))
這篇文章主要介紹了Android判斷App前臺運行還是后臺運行的相關資料,需要的朋友可以參考下2016-04-04Android形狀圖形與狀態(tài)列表圖形及九宮格圖片超詳細講解
這篇文章主要介紹了Android形狀圖形與狀態(tài)列表圖形及九宮格圖片的應用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-09-09Android實現(xiàn)單頁顯示3個Item的ViewPager炫酷切換效果
這篇文章主要為大家詳細介紹了Android實現(xiàn)單頁顯示3個Item的ViewPager炫酷切換效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10