问题
Is there a way to clear the selected item in a 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>
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.
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);
and
exampleList.SetSelection(-1);
But that does not seem to work.
回答1:
Using listView.SetItemChecked(-1, true); works fine here.
Here is my Activity I tested with:
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:
<?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>
回答2:
Use clearChoices() to clear the checked state of all items in a ListView
回答3:
Its works simple for me:
ListView listView = (ListView) findViewById(R.id.idMyListView);
listView.clearFocus();
回答4:
It's an old question, but just in case someone else needs it in the future, based on @Cheesebaron answer, here's what I did:
On each ListViews' OnItemClickListener set the other's list checked item to false:
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
list2.setItemChecked(list2.getSelectedItemPosition(), false);
}
});
This implementation is in Java, but if you get the logic here you can implement it in C# as well. Hope this helps.
回答5:
For CHOICE_MODE_SINGLE / singleChoice
int v = listView.getCheckedItemPosition();
if (v >= 0)
listView.setItemChecked(v, false);
来源:https://stackoverflow.com/questions/15081274/clear-singlechoice-listview-selection