Deselect selected item in ListView

对着背影说爱祢 提交于 2019-12-18 05:36:15

问题


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!


回答1:


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




回答2:


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.




回答3:


For me it worked with:

listView.setAdapter(myAdapter);



回答4:


In your XML where you declare the ListView use:

<ListView android:id="@+id/my_list" android:listSelector="#00000000" />



回答5:


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




回答6:


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




回答7:


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);




回答8:


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.



来源:https://stackoverflow.com/questions/17751129/deselect-selected-item-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!