2016-06-07 60 views
1

這個問題已經很多次了,我搜索了很多,但幾乎所有的解決方案都不適合我。我想選擇多個listview項目,並選擇所選項目更改的顏色。即如果在列表視圖中有10個項目,並且我選擇了5個項目,那麼這5個項目的顏色應該被改變並且處於焦點中,以便我可以對它們執行操作。但是,每個解決方案都不適合我,只有當前選定的項目發生了變化,但並非所有項目都集中併發生變化。列表視圖中的多項不專注(顏色不變)

我嘗試了許多代碼,這是其中的一個:

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

<item 
android:state_selected="false" 
android:state_pressed="false" 
android:drawable="@drawable/normal_shape" /> 

<item android:state_pressed="true" 
android:drawable="@drawable/gradient_bg_hover" /> 

<item android:state_selected="true" 
android:state_pressed="false" 
android:drawable="@drawable/gradient_bg_hover" /> 
</selector> 

gradient_bg_hover:

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

<gradient 
    android:startColor="#d9dd00" 
    android:endColor="#e6ea02" 
    android:angle="270" /> 
</shape> 

normal_shape:

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

<gradient 
    android:angle="270" 
    android:endColor="#fefeff" 
    android:startColor="#d5dbe7" /> 

<stroke 
    android:width="1dp" 
    android:color="#acd9dd" /> 

</shape> 

,並在這裏我的列表視圖是XML代碼:

<ListView 
    android:id="@+id/messageList" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginLeft="4dp" 
    android:layout_marginRight="4dp" 
    android:cacheColorHint="@null" 
    android:listSelector="@drawable/listsele" 
    android:layout_marginBottom="5dp" /> 

這是什麼問題?爲什麼由我選擇的項目不集中,不改變顏色

+0

做ü知道如何更新適配器 –

+0

通過更新你的意思是? –

+0

更改陣列並更新列表 –

回答

0

刪除此行

android:cacheColorHint="@null" 

,並將選擇作爲背景