ListViewItem does not get highlighted if the background isn't transparent

ε祈祈猫儿з 提交于 2019-12-08 00:51:46

问题


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"
    >

回答1:


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.




回答2:


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.



回答3:


Use a <selector> to display a "pressed" state with your gradient background.



来源:https://stackoverflow.com/questions/3809107/listviewitem-does-not-get-highlighted-if-the-background-isnt-transparent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!