Highlight selected items in ListFragment (MultiChoiceModeListener)?

I want to see which items were selected in my list. Before turning the ListView into a ListFragment , the selected objects had a blue background color (Holo Dark), but now I can only see the blue color by clicking on the objects - then it disappears. I have no problem with the fact that the elements are "checked" because they are. You just don't see it.

Here is what I do in onActivityCreated() :

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

LongPress() is a private class that implements ListView.MultiChoiceModeListener . When you click on an item, the contextual action panel opens. Then the user can select more items, and this is where the problem arises. You cannot see which elements are checked or not (I want them to be highlighted). Why did it work before the ListFragment extension? I tried creating a custom selector in my .xml files, which doesn't seem to work either. I would rather use a custom one if there is one.

Here is another method in the ListFragment class:

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

fragment_listview.xml does nothing special but change the background color and the color of the separator.

What should I do?

I tried this, but this did not 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> 
+4
source share
2 answers

I had the same problem.

I found a solution in this post: Highlight a custom list item with a long press

Trick:

Set the list lines in the layout file (in the top-level component, usually LinearLayout or RelativeLayout):

Android: background = "android: atr / activatedBackgroundIndicator"

+1
source

I can’t find the information right now, but you must provide a template table for listview elements that provide a separate accessible for checked .

0
source

Source: https://habr.com/ru/post/1437270/


All Articles