2013-02-26 43 views
11

有没有办法清除ListView中的选定项目?清除SingleChoice 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> 

现在我真的是在一个单一的活动2名列表视图,当一个产品在一个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); 

但是,这似乎并没有上班。

回答

25

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

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> 
+0

这似乎工作,没有试过那个,谢谢 – TimothyP 2013-03-04 01:43:00

+0

什么是=>符号?我从来没有见过在android代码中使用过。 – 2014-06-30 21:53:12

+0

@IgorGanapolsky这是一个lambda表达式,一个匿名方法。这是C#。在Java中,您有'IOnClickListener'实现。 – Cheesebaron 2014-07-01 08:40:54

15

使用clearChoices()以清除在ListView

+1

我试过,但似乎没有工作(应注意我使用单声道为Android) – TimothyP 2013-02-26 05:12:39

+10

看来,你应该调用adapter.notifyDataSetChanged()后,使其工作。 – pvshnik 2013-11-16 10:34:05

+0

这对ListFragment不起作用。 – 2014-06-30 21:55:21

2

它的工作原理简单对我来说:

ListView listView = (ListView) findViewById(R.id.idMyListView); 
     listView.clearFocus(); 
+5

为什么clearFocus()会清除单选ListView?没有意义。 – 2014-06-30 21:55:01

2

这是一个老问题,但只是以防别人将来需要它时,基于@Cheesebaron的回答,这里是我所做的:

在每个列表视图OnItemClickListener设置其他的名单核对项目为false:

list1.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int pos, long id) { 
      list2.setItemChecked(list2.getSelectedItemPosition(), false); 
     } 
}); 

这个实现是在Java中,但如果你在这里得到的逻辑,你可以在C#中实现它。希望这可以帮助。