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

半腔热情 提交于 2019-12-06 09:39:35

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.

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