2013-01-09 56 views

回答

5

保存點擊項目的位置並從getView()的適配器位置中檢查它。如果兩者都相同,則設置任何背景色或資源爲

convertView.setBackgroundColor(Color.WHITE); 
     or  
    convertView.setBackgroundResource(R.drawable.bg); 

其中convertView是適配器的虛擬佈局。

+0

謝謝,但。更改所選值時,顏色不會移除。 –

+0

在if if else條件下寫上面的代碼。也就是說,如果兩者都相同,那麼您選擇的顏色和其他默認顏色相同。它可能會幫助你。 –

+0

和無效的項目點擊列表視圖。作爲lstView.invalidateViews(); –

0

myDrawable.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- focused --> 
    <item android:state_focused="true" android:drawable="@color/YOUR_COLOR_HERE" /> 
    <!-- focused and pressed--> 
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/YOUR_COLOR_HERE" /> 
    <!-- pressed --> 
    <item android:state_pressed="true" android:drawable="@color/YOUR_COLOR_HERE" /> 
    <!-- default --> 
    <item android:drawable="@color/YOUR_COLOR_HERE" /> 
</selector> 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/myview" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="top" 
     android:background="@drawable/myDrawable"> 
     <!-- other views in layout--> 
    </LinearLayout> 

或者添加這個屬性給你的ListView

android:listSelector="@android:color/darker_gray" 
0

<item android:state_activated="true"> 
    <shape android:shape="rectangle"> 
    <solid android:color="#333333" /> 
    <padding android:left="5dp" android:right="5dp" /> 
    </shape></item> 
<item><shape android:shape="rectangle"> 
     <solid android:color="#222222" /> 
    </shape></item> 

相關問題