Monday, May 20, 2013

Re: [android-developers] ListView with "selected" state does not work when list item view has a background

Hi everybody!

On Mon, May 20, 2013 at 8:46 PM, Romain Guy <romainguy@android.com> wrote:
>
> The list selector is drawn by default under list items. You can either change it to draw on top (but you'll have to use custom drawables so the items are visible underneath) or use a background on your list items with a selected state.

I've read that before, only I seem to be doing something wrong. I've
create an ugly sample project
(https://github.com/skyflyer/android_list_selection), where I
demonstrate that when there is no background, list selector is
working, but when there is, list selector is not working, even though
the "list item background" is set to a drawable which is a selector.

To be more clear, item layout (row.xml) has:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="@drawable/row_background">

where row_background is defined like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@color/blue" />
<item android:state_focused="true" android:drawable="@color/green" />
<item android:state_selected="true" android:drawable="@color/red" />
<item android:drawable="@color/white" />
</selector>

The way I understand this is: row should be white by default. When
pressed, it should be blue (and it is!), and when selected, it should
be red (when list choiceMode is set appropriately). But that is not
the case. The row background stays the same.

Am I misunderstanding this statement: "...or use a background on your
list items with a selected state."?

Romain, if I understand correctly: list selector is drawn "under the
item view". So when there is no background (it is transparent), the
list selector "shows through". But when list item has a background,
this selector is not visible. Correct? But if I set the background of
the list item to a "selector", this selector should match
"state_selected" that should come into effect, when list item is
selected? (it does not in my case).

Regards,
Miha.

--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment