I had a similar problem , and my code looked like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="@android:integer/config_mediumAnimTime" android:exitFadeDuration="@android:integer/config_mediumAnimTime" > <item android:state_pressed="true" android:drawable="@color/pressed" /> <item android:drawable="@color/default" /> </selector>
First I found a hint to get rid of enterFadeDuration and use only exitFadeDuration . This resolved the initial invisibility problem, but the view nevertheless disappeared into invisibility during the first intervention.
Then I modified my structure as follows:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/default" /> <item> <selector android:enterFadeDuration="@android:integer/config_mediumAnimTime" android:exitFadeDuration="@android:integer/config_mediumAnimTime" > <item android:state_pressed="true" android:drawable="@color/pressed" /> </selector> </item> </layer-list>
Basically, I just popped the default selection out of the selector. This is a workaround, and it also works for multi-state selectors, but has some notable limitations:
- by default it is always displayed as a lower level. It works for opaque colors, but transparency can cause unwanted results.
- If the view starts in one of the states checked by the selector, it is still displayed by default , because the selector still starts as invisible.
It may not be applicable to the original problem, but it is something that needs to be considered to overcome this selector behavior.
source share