Is it possible to apply a custom background to each Listview item via the list selector?
The default selector specifies @android:color/transparent
for the state_focused="false"
case, but changing this to some custom drawable doesn't affect items that aren't selected. Romain Guy seems to suggest in this answer that this is possible.
I'm currently achieving the same affect by using a custom background on each view and hiding it when the item is selected/focused/whatever so the selector is shown, but it'd be more elegant to have this all defined in one place.
For reference, this is the selector I'm using to try and get this working:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:drawable="@drawable/list_item_gradient" />
<!-- 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="@drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false"
android:drawable="@drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/list_selector_background_transition" />
<item android:state_focused="true"
android:drawable="@drawable/list_selector_background_focus" />
</selector>
And this is how I'm setting the selector:
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="@drawable/list_selector_background" />
Thanks in advance for any help!
FrostWire Team over here.
All the selector crap api doesn't work as expected. After trying all the solutions presented in this thread to no good, we just solved the problem at the moment of inflating the ListView Item.
Make sure your item keeps it's state, we did it as a member variable of the MenuItem (boolean selected)
When you inflate, ask if the underlying item is selected, if so, just set the drawable resource that you want as the background (be it a 9patch or whatever). Make sure your adapter is aware of this and that it calls notifyDataChanged() when something has been selected.
It'd be really nice if the selectors would actually work, in theory it's a nice and elegant solution, but it seems like it's broken. KISS.
I've been frustrated by this myself and finally solved it. As Romain Guy hinted to, there's another state,
"android:state_selected"
, that you must use. Use a state drawable for the background of your list item, and use a different state drawable forlistSelector
of your list:list_row_layout.xml:
listitem_background.xml:
layout.xml that includes the ListView:
listitem_selector.xml:
The solution by dglmtn doesn't work when you have a 9-patch drawable with padding as background. Strange things happen, I don't even want to talk about it, if you have such a problem, you know them.
Now, If you want to have a listview with different states and 9-patch drawables (it would work with any drawables and colors, I think) you have to do 2 things:
What you should do is first set the row_selector.xml:
Don't forget the
android:state_selected
. It works likeandroid:state_focused
for the list, but it's applied for the list item.Now apply the selector to the items (row.xml):
Make a transparent selector for the list:
This should do the thing.
I always use the same method and it works every time, every where: I simply use a selector like this
and set on the ListView (THIS IS VERY IMPORTANT TO MAKE IT WORKING) the attribute
for the textColor just put in the color folder(IMPORTANT, not drawable folder!) a selector like this
this a sample row template
everything showld work without tedious workarounds. hope this help
It's enough,if you put in
list_row_layout.xml
:listitem_selector.xml:
The article "Why is my list black? An Android optimization" in the Android Developers Blog has a thorough explanation of why the list background turns black when scrolling. Simple answer: set cacheColorHint on your list to transparent (#00000000).