ListViewItem does not get highlighted if the background isn't transparent
I have a ListViewItem with a custom gradient background. The default selector no longer highlights these rows anymore.
Highlight only works if I set the background to transparent. How can I get the highlight without this change?
Here is my ListView:
<ListView android:id="@+id/symbolsListView"
android:layout_width="fill_parent"
android:background="@drawable/transparent_background"
android:layout_height="390dp"
android:divider="@drawable/ui_divider_line"
android:cacheColorHint="#00000000"
开发者_运维问答android:listSelector="@drawable/blue"
android:drawSelectorOnTop="true"
>
your some states that are focused and window_focused. Below i mentioned the code for selector try this.
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="@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="@color/transparent" />
<item android:state_focused="true" android:state_enabled="false"
android:drawable="@color/transparent" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@color/solid_red" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@color/solid_red" />
<item android:state_focused="true" android:drawable="@color/solid_red" />
</selector>
Hope it works.
The current selection in a ListView is highlighted by an additional drawable that you provide using the "listSelector" attribute.
By default, the selector drawable is drawn behind the list item. Therefore, if the gradient background of yout item is opaque, the selector will never be shown.
There are 2 solutions :
- Make the background gradient transparent by lowering alpha values.
- Set the "drawSelectorOnTop" attribute to true, so that the selector is drawn on top of the list item. In this case, make sure your selector drawable is not fully opaque.
Use a <selector>
to display a "pressed" state with your gradient background.
精彩评论