清除SingleChoice ListView选择 [英] Clear SingleChoice ListView selection
问题描述
是否可以清除ListView中的选定项目?
Is there a way to clear the selected item in a ListView?
ListView的定义如下:
The ListView is defined like this:
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="50dp"
android:id="@+id/example_list"
android:layout_weight="2"
android:choiceMode="singleChoice"/>
并使用自定义适配器填充.
And is filled using a custom Adapter.
使用选择器突出显示所选项目:
The selected item is highlighted using a Selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#3E5260"
android:endColor="#3E5260"
android:angle="270" />
</shape>
</item>
<item android:state_activated="true">
<shape>
<gradient
android:startColor="#3E5260"
android:endColor="#3E5260"
android:angle="270" />
</shape>
</item>
</selector>
现在我真正拥有的是在单个活动中以及项目为2个时的ListViews
我要在另一个ListView中取消选择该项目.
Now what I really have is 2 ListViews in a single activity and when an item is
selected in one ListView I want to deselect the item in the other ListView.
单击一个项目时,两个ListViews都会引发以下处理程序:
Both ListViews raise the following handler when an item is clicked:
void DeviceList_Click(object sender, EventArgs e)
{
//easy enough to check which ListView raised the event
//but then I need to deselect the selected item in the other listview
}
我已经尝试过类似的事情:
I've tried things like:
exampleList.SetItemChecked(exampleList.SelectedItemPosition, false);
和
exampleList.SetSelection(-1);
但这似乎不起作用.
推荐答案
在这里使用listView.SetItemChecked(-1, true);
可以正常工作.
Using listView.SetItemChecked(-1, true);
works fine here.
这是我测试过的活动:
SetContentView(Resource.Layout.Main);
var listView = FindViewById<ListView>(Resource.Id.listView);
_listAdapter = new CustomListAdapter(this);
listView.Adapter = _listAdapter;
var button = FindViewById<Button>(Resource.Id.removeChoice);
button.Click += (sender, args) => listView.SetItemChecked(-1, true);
Main.axml:
Main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
/>
<Button
android:id="@+id/removeChoice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="remove choice"
/>
</LinearLayout>
这篇关于清除SingleChoice ListView选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!