howettl
howettl

Reputation: 12518

GridView item selector checked state not appearing

I have a GridView with a bunch of items that are being populated using a custom adapter. The grid view is set to CHOICE_MODE_MULTIPLE_MODAL in java, and I'm able to select things using the contextual action bar (all of this works fine).

I want the grid items to highlight when pressed and have a different highlight when selected (exactly the behavior you'll see in the Gallery app in ICS).

I have a selector which is being specified in the grid view XML like so: listSelector="@drawable/grid_item_selector". I have also specified android:drawSelectorOnTop="true". Here is the selector XML:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/grid_item_selected" android:state_activated="true"/>
<item android:drawable="@drawable/grid_item_selected" android:state_checked="true"/>
<item android:drawable="@drawable/grid_item_selected" android:state_selected="true"/>
<item android:drawable="@drawable/grid_item_pressed" android:state_pressed="true"/>
<item android:drawable="@android:color/transparent"/>

</selector>

The pressed state works perfectly. However, the checked/selected states never appear.

Even if I set an item to be checked in my java code, the checked state never appears.

I can't set the selector as the background of the grid items themselves because I need the selected state drawable to be the foreground, not the background.

Upvotes: 2

Views: 2319

Answers (1)

MrChaz
MrChaz

Reputation: 1095

The selector only shows on the thing that you're touching. Once you take your finger off it no longer displays.

So, what I've ended up doing is having a View at the bottom of the layout (so it appears on top) with a background set to a selector. The selector only has anything for state_selected. The OS handles the rest.

Not exactly the nicest, but it works...

Upvotes: 1

Related Questions