0

I have a ListFragment whose ListView is set to ListView.CHOICE_MODE_SINGLE. The adapter returns a custom row layout and I'm setting the background of each row layout to a selector.

But I can't seem to define the selector so that on a long-press, the row stays highlighted, but on a normal press, it just highlights briefly.

Here is my selector:

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

    <item android:state_activated="true" android:drawable="@color/light_blue" />
    <item android:drawable="@android:color/transparent" />

</selector>

The row stays highlighted on long-press, but a normal press also makes the row stay highlighted. I've tried using various combinations of attributes in each item but can't get it right.

Clarification: I would still like it so that when another row is pressed, the previously long-pressed (and highlighted) row becomes un-highlighted. And the above selector does do that.

JDJ
  • 4,298
  • 3
  • 25
  • 44
  • Oh boy, you're going to have to make some customer states if that's really the approach you want to take. You might be better off storing long pressed items in a list, and in your adapters `getView` set a different background if your item is in the `wasLongPressed` list. – MeetTitan Dec 17 '14 at 20:43
  • So you don't think this is possible to define in a selector? – JDJ Dec 17 '14 at 20:47
  • It's possible. It just might be a headache defining them. – MeetTitan Dec 17 '14 at 20:49
  • Use `CHOICE_MODE_NONE` for normal operation. When the user long-clicks a row, switch into `CHOICE_MODE_MULTIPLE_MODAL` and show your action mode (or whatever). Then, when the user dismisses the action mode (or whatever), switch back to `CHOICE_MODE_NONE`. See https://github.com/commonsguy/cw-omnibus/tree/master/ActionMode/LongPress for an example of this. – CommonsWare Dec 17 '14 at 20:53
  • Okay let me try out these suggestions and the answer below and I'll report back. – JDJ Dec 17 '14 at 20:57

1 Answers1

0

If you want to use states, look at the default states android defines to make sure there isn't one already. If android doesn't have a state that suits your needs, take a look at defining custom states here How to add a custom button state

If you don't want to do that you can always add selections to a list and change backgrounds in getView as my comment hints.

Community
  • 1
  • 1
MeetTitan
  • 3,383
  • 1
  • 13
  • 26