2011-02-14 25 views
0

我正在使用此tutorials,我想爲此列表視圖添加選擇器。我嘗試了一些代碼,但它的工作。我該怎麼做。自定義列表視圖的選擇器「更改ListView背景 - 奇怪的行爲」

我使用的代碼作爲list_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_focused="true" 
     android:drawable="@drawable/list_selector_background_focus" /> 
    <item android:state_pressed="true" 
     android:drawable="@drawable/list_selector_background_pressed" /> 

</selector> 

和我的列表視圖

<ListView 
       android:id="@+id/select_names_tags_lv" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:listSelector="@drawable/list_selector" 
       android:layout_alignParentTop="false" 
       android:layout_gravity="center_vertical"> 
      </ListView> 

回答

2

查看以下鏈接。這裏我提到了一個完整的代碼來正確使用listview。使用這個我們可以實現任何列表視圖行爲。我們也可以嵌入動畫。

Change ListView background - strange behaviour

希望這有助於:)

+0

我要選擇,我不會只點擊後改變背景事件。我想要列表視圖作爲默認列表視圖,其中選擇器適用於焦點,單擊和默認事件。 – 2011-02-14 08:30:22

+0

android:background =「@ drawable/list_selector」 將這個屬性放在xml中,用於在getView方法中膨脹的單元佈局,而不是選擇listview的背景。 – Javanator 2011-02-14 09:07:53

0

剛剛從listSelector此處更改一個字的背景。因爲你想要它作爲背景可繪製選擇器,對嗎?

 <ListView 
      android:id="@+id/select_names_tags_lv" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
       android:background="@drawable/list_selector" 
      android:layout_alignParentTop="false" 
      android:layout_gravity="center_vertical"> 
     </ListView> 

編輯:

試着改變你的選擇文件(我改變了這兩個項目的順序):

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
     android:drawable="@drawable/list_selector_background_pressed" /> 
    <item android:state_focused="true" 
     android:drawable="@drawable/list_selector_background_focus" /> 
</selector> 
相關問題