2013-01-17 42 views
0

嗯,我想突出顯示視圖,以便用戶可以看到它選擇了哪個項目。我想是這樣的:如何突出顯示ListView對點擊的多個視圖?

View.setBackground(COLOR.RED); 

裏面的ListView項目上點擊監聽器和它的作品,但是如果名單得到滾動隨機的觀點開始改變的背景。我如何突出顯示它們,並且不會丟失listView滾動中突出顯示的項目?

注:我使用的是自定義適配器與一個ImageView的和每行3個TextView的。

感謝

編輯:對不起忘了說,我希望能夠選擇多個項目。

回答

1

設置項目佈局的背景顏色?看看這裏https://github.com/cplain/custom-list的概念應該是相同的 - 只是忽略我可運行

+0

請看看我的編輯和遺憾 –

+0

我figuired這是你想要的。在我鏈接的回購中,看看如何隨着可運行進程更改列表項目的背景。所有你應該需要做的是在'onItemSelected()' – seaplain

+0

完美,容易和簡單的解決方案,同類型的代碼!我不敢相信這很簡單,我沒有想到它! –

1

一個快速的方法做,這是創建幾個自定義樣式;在繪製文件夾,您可以創建正常和懸停樣式或壓入狀態:

所以在../drawable/你想幾個要素:

1)list_bg.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="#db0000" 
     android:centerColor="#c50300" 
     android:endColor="#b30500" 
     android:angle="270" /> 
</shape> 

2)list_bg_hover.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="#db0000" 
     android:centerColor="#c50300" 
     android:endColor="#b30500" 
     android:angle="270" /> 
</shape> 

3)list_selector.xml文件:現在

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

    <item android:drawable="@drawable/list_bg" android:state_pressed="false" android:state_selected="false"/> 
    <item android:drawable="@drawable/list_bg_hover" android:state_pressed="true"/> 
    <item android:drawable="@drawable/list_bg_hover" android:state_pressed="false" android:state_selected="true"/> 

</selector> 

,爲了使用這一切,你需要做的就是附加的風格佈局像這樣的ListView行項目,android:listSelector="@drawable/list_selector"和應該做的招。

+0

請參閱我的編輯和對不起 –