I have a single selection ListView
, with each row having selector. The selector behavior is as follow. Note, the ListView
itself also placed on a white color LinearLayout
.
- During normal state, the row will be white color.
- During "selecting process", I want to let the system to handle the coloring. So, I mark them as transparent.
- When a row item is being selected, I want the highlight color to be blue.
- I would like to have some animation when I select from this row to that row, so I'm having
android:exitFadeDuration
Here is how my selector looks like
<selector android:exitFadeDuration="@android:integer/config_mediumAnimTime"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@drawable/blue" />
<!-- http://stackoverflow.com/questions/2217753/changing-background-color-of-listview-items-on-android -->
<item android:state_pressed="true" android:drawable="@android:color/transparent" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="@android:color/transparent" /> <!-- focused -->
<item android:state_selected="true" android:drawable="@android:color/transparent" /> <!-- selected -->
<item android:drawable="@drawable/white" />
</selector>
When I perform selection through Java code, the fading animation effect looks perfect
// Highlighting.
listView.performItemClick(getListView().getAdapter().getView(index, null, null), index, index);
listView.post(new Runnable() {
@Override
public void run() {
listView.setSelection(index);
}
}
However, if I perform manual selection by clicking on the ListView
explicitly, I can see a slightly flickering effect!
May I know why is it so? Is there any way I can avoid this? I realize even I change the transparent state to
<item android:state_pressed="true" android:drawable="@drawable/blue" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="@drawable/blue" /> <!-- focused -->
<item android:state_selected="true" android:drawable="@drawable/blue" /> <!-- selected -->
flickering still happen. Note, flickering effect won't be there if there is no android:exitFadeDuration
. However, removing android:exitFadeDuration
is not something I want.