ListView item with list_selector_background as LayerList drawable

故事扮演 提交于 2019-12-20 01:05:49

问题


If we use a ListView or an ExpandableListView the default background is @android:drawable/list_selector_background. I have an ExpandableListView which shows data grouped by date. I like the state list list_selector_background drawable and want to keep the behaviour for different states. But for the weekend (for some of the list view items) i'd like to define a custom background. But these items should still use the drawables for different states defined through list_selector_background. I saw different not suitable answers. Most of them say i should define my own state list drawable. The list_selector_background.xml defines, besides different drawable for different states, <item android:state_window_focused="false" android:drawable="@color/transparent" />

So i thought i define my own state list drawable and just change the transparent to my desired color. The problem is the drawables used for the states are not public. I really want to stay close to the android style so i don't want to write my own state list drawable with own drawables for states.

At the end i wrote a solution using a layer list drawable.

<?xml version="1.0" encoding="utf-8"?>
  <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item 
       android:drawable="@color/DarkBlue"
       android:id="@+id/background" />

    <item 
       android:drawable="@android:drawable/list_selector_background"
       android:id="@+id/list_selector_background" />

  </layer-list>

This perfectly matches the desired behaviour with one exception :/. I have my custom background and the item reacts with drawable defined in list_selector_background for press events. But somehow the focused state does not use the drawable defined by list_selector_background (<item android:state_focused="true" android:drawable="@drawable/list_selector_background_focus" />)

So i kindly ask if anyone can imagine why the focused state does not work? I am developing with API level 8. If you need any further information i'd like to post. Thanks

来源:https://stackoverflow.com/questions/7108451/listview-item-with-list-selector-background-as-layerlist-drawable

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