2014-09-26 97 views
1

我無法使用android中的自定義適配器突出顯示多選列表視圖。激活和按下狀態工作得很好。我想要的是,當用戶點擊一個應該突出顯示的項目時。多選擇ListView的選擇器

new_list_selector.xml

<?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_pressed" /> 
    <item android:state_activated="true" android:drawable="@drawable/list_selector_selected" /> 
    <item android:state_selected="true" android:drawable="@drawable/list_selector_pressed" /> 
</selector> 
在上面的XML

,爲 「激活」 和 「壓」 的狀態工作,但不是 「選擇」。

在我listview_item.xml我將背景設置爲選擇器如下

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:background="@drawable/new_list_selector"> 
<!--list items goes here--> 
</RelativeLayout> 

我這樣定義

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/masterContainer"> 
<ListView 
     android:minWidth="25px" 
     android:minHeight="25px" 
     android:focusable="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/friendViewList" /> 
</RelativeLayout> 

我得到的激活狀態,當我點擊並按住我的列表視圖一個列表項。按下狀態時,我點擊列表項目。我希望用戶點擊該項目時突出顯示。有人可以幫我解決這個問題嗎?感謝您提前提出任何意見。 PS:我更喜歡xml解決方案,而不是編碼。我還與state_focused組試圖選擇以true沒有運氣

+0

然後不要將其設置爲佈局的背景。取而代之的是將其呈現在其中的項目的背景。 – 2014-09-26 06:02:50

+0

感謝您的輸入。這確實是列表視圖中顯示的項目的佈局。這個佈局包含一個圖像和兩個文本視圖,我爲了簡單而刪除了它 – 2014-09-26 06:07:20

回答

0

執行以下操作:

listview_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="48dp"> 
<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/new_list_selector"/> 

</RelativeLayout> 

這將設置選擇上ImageView的。你也可以用你的兩個TexTviews來做。

+0

我一定會嘗試一下,然後回覆你 – 2014-09-26 06:17:46

+0

只是試了一下。不幸的是,這並沒有解決我的問題 – 2014-09-26 06:25:29

+0

你用ImageView試過嗎?你是否動態覆蓋ImageView的背景? – 2014-09-26 06:34:45