3

這是我使用的列表項選擇:列表項長按過渡

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
<item android:drawable="@color/red" android:state_pressed="true"/> 
<!-- pressed --> 
<item android:drawable="@color/green" android:state_pressed="false"/> 
<!-- normal --> 
</selector> 

我已經啓用上下文菜單爲ListView項目,使一個用戶可以長按一個項目。我想要的是當用戶長時間點擊一個項目時,顏色應該從綠色變爲紅色。我怎樣才能做到這一點?

+1

您可以使用['Transition(Drawable)'](http://developer.android.com/guide/topics/resources/drawable-resource.html#Transition)來完成此操作。對於一個真實的例子,看看作爲列表選擇器的一部分的[transition](https://github.com/JakeWharton/ActionBarSherlock/blob/master/actionbarsherlock/res/drawable/abs__list_selector_background_transition_holo_light.xml) ActionBarSherlock。 –

回答

2

您可以使用list_selector_background因爲,作爲建議here

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:state_window_focused="false" android:drawable="@android:color/transparent" /> 
     <!-- 
       Even though these two point to the same resource, have two states so 
       the drawable will invalidate itself when coming out of pressed state. 
     --> 
     <item android:state_focused="true" android:state_enabled="false" 
       android:state_pressed="true"  android:drawable="@drawable/list_selector_background_disabled" /> 
     <item android:state_focused="true" android:state_enabled="false" 
       android:drawable="@drawable/list_selector_background_disabled" /> 
     <item android:state_focused="true" android:state_pressed="true" 
       android:drawable="@drawable/list_selector_background_transition" /> 
     <item android:state_focused="false" android:state_pressed="true" 
       android:drawable="@drawable/list_selector_background_transition" /> 
     <item android:state_focused="true" 
       android:drawable="@+drawable/list_selector_background_focus" /> 
</selector> 

並使用長按的轉變,作爲建議here: -

<transition xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/list_selector_background_pressed" /> 
    <item android:drawable="@drawable/list_selector_background_longpress" /> 
</transition> 

可能它會幫助你..

+0

我試過了。沒有幫助! –

+0

你弄明白了嗎?我有同樣的問題,代碼看起來不錯,但轉換沒有發生,只有第一種顏色在長按使用。 –