清除 SingleChoice ListView 选择 [英] Clear SingleChoice ListView selection

查看:23
本文介绍了清除 SingleChoice ListView 选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法清除 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 中选择我想取消选择另一个 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.

当单击一个项目时,两个 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
}

我尝试过以下方法:

exampleList.SetItemChecked(exampleList.SelectedItemPosition, false);

exampleList.SetSelection(-1);

但这似乎不起作用.

推荐答案

Using 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:

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆