Deselect selected item in ListView
Asked Answered
T

8

19

I use a ListView in my layout like this:

 <ListView android:id="@+id/list"
              android:layout_width="fill_parent"
              android:layout_gravity="center"
              android:layout_height="match_parent"
              android:layout_weight="0.7"
              android:layout_marginTop="5dp"
              android:orientation="vertical"
              android:layout_centerInParent="true"
              android:divider="@color/dark_grey"
              android:drawSelectorOnTop="false"
              android:focusable="true"
              android:layout_marginBottom="55dp"
              android:cacheColorHint="#00000000"
              android:listSelector="@color/light_grey"
              android:dividerHeight="1px" />

The selector works great but how can I disable the selector?

I tried:

listView.clearChoices();
listView.setSelected();
listView.setSelector();
...

and a few more things but nothing works. Any ideas how I can turn my selected item back to normal? Can't be that complicated, right?

Edit: I need a programmatical solution!

Tamper answered 19/7, 2013 at 16:31 Comment(3)
May your are suffering from this bug #9754670Cheapjack
Yeah, when calling requestLayout() after clearChoices() it works. If you post an anwer I'll accept itTamper
Does this answer your question? How do I clear ListView selection?Seismic
C
34

Call requestLayout() on the ListView after clearChoices(). It will work.

Cheapjack answered 22/7, 2013 at 7:49 Comment(4)
for me it worked with adapter.notifyDataSetChanged() after clearChoices()Jerrybuilt
For me worked with listviewObject.clearChoices () after adapterObject.notifyDataSetChanged ()Nightfall
adapter.notifyDataSetInvalidated() works for ExpandableListViewBreadboard
Let's warm up this topic. I'm trying to use this in the onDestroyActionMode method of the ActionMode.Callback and doesn't work. The only visible change happens with calling the listView.setAdapter(myAdapter); but it repositioning the screen to the top unexpectedly.Lashonda
P
12

As @Muerte mentioned above, doing the following worked for me:

myListView.clearChoices();
myAdapter.notifyDataSetChanged();

That seems like it would be better than redrawing the whole layout.

Perishing answered 22/5, 2014 at 11:20 Comment(1)
For me worked with listviewObject.clearChoices () after adapterObject.notifyDataSetChanged ()Nightfall
D
4

For me it worked with:

listView.setAdapter(myAdapter);
Duplicator answered 20/5, 2016 at 19:55 Comment(3)
Thank you. After trying so many different way, this finally worked for me.Photoflood
This worked for me. All the other suggestions didn't work. I'm not sure why they didn't but perhaps its because I'm calling myAdapter.notifyDataSetChanged() in the onItemClick() handler.Yevetteyew
The drawback of this solution is that the list will be recreated positioning the screen to the top. It might be annoying sometimes.Lashonda
L
0

In your XML where you declare the ListView use:

<ListView android:id="@+id/my_list" android:listSelector="#00000000" />
Lamont answered 19/7, 2013 at 16:35 Comment(2)
yeah, but i want my selector to work again for the next click on a list item... if I do this then the selector is invisible, right?Tamper
Oh maybe this can help..Lamont
H
0

You can set a selector after click:

<?xml version="1.0" encoding="utf-8"?>

<item android:drawable="@drawable/transparent_white" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/transparent_white" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/transparent_white" android:state_focused="true"/>
<item android:drawable="@drawable/transparent_white" android:state_focused="false"/>

and the @drawable/transparent_white is #00ffffff

Hideaway answered 19/7, 2013 at 16:39 Comment(0)
H
0

try this

Create your own selector xml file like this

YourSelector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false" android:state_focused="true"
        android:drawable="@drawable/item_disabled" />
  <item android:state_pressed="true"
        android:drawable="@drawable/item_pressed" />
  <item android:state_focused="true"
        android:drawable="@drawable/item_focused" />
</selector>

check out this link

here

Headword answered 19/7, 2013 at 16:44 Comment(0)
E
0

Nothing of the solutions actually worked for me. My listview is for listing the searched items and has a provision to redo the search. On getting the result I added the following code snippet and it worked :)

myAdapter.setSelectedIndex(-1);

Electrolytic answered 14/3, 2017 at 6:8 Comment(1)
What type is your myAdapter? I couldn't find this method neither for ListView nor for ArrayAdapter.Lashonda
S
0

Your selector resource seems to be wrong here. You should use a drawable with transparent background, and i think this is what actually colors your rows, but not choiceMode of the ListView. Please check this another answer for more details.

Seismic answered 28/1, 2018 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.