已解決:ListView點擊Item無響應問題,親測有效

 如果listitem裏面包括button或者checkbox等控件,默認情況下listitem會失去焦點,導致無法響應item的事件,最常用的解決辦法

是在listitem的佈局文件中設置descendantFocusability屬性。

item的佈局文件:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="wrap_content"  
  5.   android:layout_height="wrap_content"  
  6.   android:paddingTop="10dp"  
  7.   android:paddingBottom="10dp"  
  8.   android:paddingLeft="5dp"  
  9.   android:paddingRight="5dp"  
  10.   android:descendantFocusability="blocksDescendants"><!--添加這個屬性-->  
  11.   <CheckBox  
  12.    android:id="@+id/history_item_checkbt"  
  13.    android:layout_height="30dp"  
  14.    android:layout_width="wrap_content"  
  15.    android:layout_centerVertical="true"  
  16.    android:layout_alignParentLeft="true"  
  17.    android:checked="false"  
  18.    >  
  19.   </CheckBox>  
  20.   
  21.   <ImageView  
  22.    android:id="@+id/history_item_image"  
  23.    android:layout_width="wrap_content"  
  24.    android:layout_height="wrap_content"  
  25.    android:layout_centerVertical="true"  
  26.    android:layout_toRightOf="@id/history_item_checkbt"  
  27.    android:background="@drawable/item_icon">  
  28.   </ImageView>  
  29.   
  30.     
  31.   <Button  
  32.    android:id="@+id/history_item_edit_bt"  
  33.    android:layout_alignParentRight="true"  
  34.    android:layout_width="wrap_content"  
  35.    android:layout_height="wrap_content"  
  36.    android:layout_centerVertical="true"  
  37.    android:text="編輯"  
  38.    android:textColor="#ffffff"  
  39.    android:textSize="14sp"  
  40.    android:background="@drawable/button_bg">  
  41.   </Button>  
  42.   
  43.   <TextView  
  44.    android:id="@+id/history_item_time_tv"  
  45.    android:layout_width="wrap_content"  
  46.    android:layout_height="wrap_content"  
  47.    android:layout_centerVertical="true"  
  48.    android:textColor="#565C5D"  
  49.    android:textSize="14sp"  
  50.    android:text="10-01 10:20"  
  51.    android:layout_marginRight="5dp"  
  52.    android:layout_toLeftOf="@id/history_item_edit_bt">  
  53.   </TextView>  
  54.   
  55.   <TextView  
  56.    android:id="@+id/history_item_title_tv"  
  57.    android:layout_height="wrap_content"  
  58.    android:layout_width="fill_parent"  
  59.    android:layout_centerVertical="true"  
  60.    android:textColor="#565C5D"  
  61.    android:textSize="14sp"  
  62.    android:text="xxxxxxxxXXXXXXXXXXXXXXXX"  
  63.    android:ellipsize="end"  
  64.    android:maxLines="1"  
  65.    android:layout_toRightOf="@id/history_item_image"  
  66.    android:layout_toLeftOf="@id/history_item_time_tv"  
  67.    android:layout_marginLeft="3dp">  
  68.   </TextView>  
  69. </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會優先其子類控件而獲取到焦點

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

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

  我們使用的是第三個。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章