清除SingleChoice ListView选择

Tim*_*hyP 14 android android-layout xamarin.android

有没有办法清除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"/>
Run Code Online (Sandbox Code Playgroud)

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

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

<?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>
Run Code Online (Sandbox Code Playgroud)

现在我真正拥有的是单个活动中的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
}
Run Code Online (Sandbox Code Playgroud)

我尝试过这样的事情:

exampleList.SetItemChecked(exampleList.SelectedItemPosition, false);
Run Code Online (Sandbox Code Playgroud)

exampleList.SetSelection(-1);
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用.

Che*_*ron 27

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

这是我测试的活动:

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);
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)


ian*_*ake 17

使用clearChoices()清除ListView中所有项的已检查状态

  • 看来你应该调用adapter.notifyDataSetChanged()来使其工作. (11认同)