Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deselect selected item in ListView

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!

Thanks!

like image 402
Ron Avatar asked Jul 19 '13 16:07

Ron


3 Answers

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

like image 138
M-WaJeEh Avatar answered Oct 07 '22 17:10

M-WaJeEh


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.

like image 23
Suragch Avatar answered Oct 07 '22 17:10

Suragch


For me it worked with:

listView.setAdapter(myAdapter);
like image 4
Ricardo Ruiz Romero Avatar answered Oct 07 '22 17:10

Ricardo Ruiz Romero