在ListView中取消选择所选项目

时间:2013-07-19 16:31:29

标签: android android-layout android-listview android-selector

我在布局中使用ListView,如下所示:

 <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" />

选择器效果很好,但如何禁用选择器呢?

我试过了:

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

还有一些东西,但没有任何作用。我有什么想法可以将我选择的项目恢复正常吗?不能那么复杂吧?

编辑:我需要一个程序化的解决方案!

谢谢!

8 个答案:

答案 0 :(得分:32)

requestLayout()之后ListView上致电clearChoices()。它会起作用。

答案 1 :(得分:11)

正如上面提到的@Muerte,执行以下操作对我有用:

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

这似乎比重新绘制整个布局更好。

答案 2 :(得分:3)

对我来说,它适用于:

listView.setAdapter(myAdapter);

答案 3 :(得分:0)

在您声明ListView的XML中使用:

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

答案 4 :(得分:0)

点击后可以设置选择器:

<?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"/>

和@ drawable / transparent_white是#00ffffff

答案 5 :(得分:0)

试试这个

像这样创建自己的选择器xml文件

  

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>

查看此链接

here

答案 6 :(得分:0)

没有任何解决方案对我有效。我的列表视图用于列出搜索到的项目,并提供重做搜索的条款。在获得结果后,我添加了以下代码片段并且它可以工作:)

myAdapter.setSelectedIndex(-1);

答案 7 :(得分:0)

你的选择器资源在这里似乎是错误的。你应该使用一个透明背景的drawable,我认为这实际上是你的行的颜色,而不是choiceMode的{​​{1}}。有关详细信息,请查看this其他答案。