4

I want to be able to see what items that have been selected in my list. Before turning ListView into ListFragment, the selected items had a blue background color (Holo Dark), but now I can only see the blue color while pressing the items - then it disappears. I do not have a problem with the items getting "checked", because they are. You just can't see it.

Here's what I do in onActivityCreated():

getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setItemsCanFocus(false);
getListView().setMultiChoiceModeListener(new LongPress());

LongPress() is a private class implementing ListView.MultiChoiceModeListener. It opens up a contextual action bar when holding finger on an item. The user can then select more items, and this is where the problem occurs. You can not see which items are checked or not (I want them to be highlighted). Why did it work before extending ListFragment? I tried to create a custom selector in my .xml-files, that didn't seem to work either. I would prefer using the custom one, if there's any.

Here's another method in my ListFragment class:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_listview, container, false);
}

fragment_listview.xml isn't doing anything special apart from changing the background color and the color of the divider.

What should I do?

I tried this, but it didn't work:

In my list_item.xml:

android:background="@drawable/selector"

selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
      android:drawable="@drawable/item_checked" />
</selector>

item_checked.xml

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

<solid android:color="#000000" />

</shape>
  • I don't know if you're still looking for an answer, but I posted one in [another thread](http://stackoverflow.com/a/13525346/304876). – Pierre-Luc Paour Nov 23 '12 at 10:32

2 Answers2

1

I was having the exact same issue.

I found a solution in this post: Highlight custom listview item when long click

The trick:

Set in the layout file of your list's row (in the top level component, usually a LinearLayout or RelativeLayout):

android:background="?android:attr/activatedBackgroundIndicator"

Community
  • 1
  • 1
mparkes
  • 332
  • 3
  • 12
0

I cannot find the details right now, but you have to provide a statelist drawable for the listview-items that provides a separate drawable for the checked state.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • I tried it, but it didn't seem to work. I edited my question with the changes I did. Maybe I misunderstood? – Linus Karlsson Oct 01 '12 at 20:20
  • Hm, yes that's what I had in mind. I remember it to have worked for me. Perhaps it has something to do with the multiselect you're using. I only had single-selection going. – Ridcully Oct 02 '12 at 06:45