gpt4 book ai didi

android - 清除 SingleChoice ListView 选择

转载 作者:可可西里 更新时间:2023-11-01 18:49:42 27 4
gpt4 key购买 nike

有没有办法清除 ListView 中的选定项?

ListView 是这样定义的:

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

并使用自定义适配器填充。

使用选择器突出显示所选项目:

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

现在我真正拥有的是单个 Activity 中的 2 个 ListViews 以及一个项目
在一个 ListView 中选择我想在另一个 ListView 中取消选择该项目。

当单击一个项目时,两个 ListView 都会引发以下处理程序:

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
}

我试过这样的事情:

exampleList.SetItemChecked(exampleList.SelectedItemPosition, false);

exampleList.SetSelection(-1);

但这似乎行不通。

最佳答案

使用 listView.SetItemChecked(-1, true); 在这里工作正常。

这是我测试过的 Activity :

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

主要.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>

关于android - 清除 SingleChoice ListView 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15081274/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com