Android ListView item blinks on selection
I am creating a custom ListView as below
Main Layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ListView android:id="@+id/listview"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="3dip"
android:fadingEdge="none"
android:soundEffectsEnabled="true"
android:clickable="true"
android:scrollbars="none"
android:divider="#ffcccccc"
android:dividerHeight="1dip"
android:layout_weight="1"/>
</LinearLayout>
Custom ListView
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="w开发者_Go百科rap_content"
android:orientation="horizontal"
android:background="@drawable/sellistitem">
<TextView
android:id="@+id/ListItem"
android:layout_width="wrap_content"
android:layout_height="30dip"
android:layout_margin="1dip"
android:textColor="@color/BLACK"
android:gravity="center_vertical"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="5dip"/>
</RelativeLayout>
and Selector as below
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/BLUE" />
<item android:drawable="@drawable/WHITE" />
</selector>
The Drawable
<resources>
<drawable name="WHITE">#ffffff</drawable>
<drawable name="BLUE">#ff0033cc</drawable>
</resources>
Having done this the selection part works fine. However when I click on any item ,the selected item blinks twice before actually changing to blue. It doesn't change to blue directly as it should. How can I remove this blinking effect?
Regards,
try this android:cacheColorHint="#00000000"
to listview
Selectors are tricky, in general. Try this for your selector instead:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/selected_background" />
<item android:state_focused="false"
android:state_pressed="false"
android:state_selected="false"
android:state_checked="false"
android:state_enabled="true"
android:state_checkable="false"
android:drawable="@drawable/normal_background"/>
</selector>
Note that the android:drawable attribute
does not have to be a drawable. If it's a simple color you can reference it with the @color/background_color
format.
EDIT:
Ok, those white and blue "drawables" aren't drawables, they're just colors, you're better off defining them in the colors.xml
. It could be that that is actually the reason for the flickering.
<resources>
<color name="blue">#ff0033cc</color>
<color name="white">#ffffff</color>
</resources>
精彩评论