我有一个ListView (从技术上讲它是一个ListFragment,但我不认为这改变了问题),它使用SimpleCursorAdapter来显示结果。
adapter = new SimpleCursorAdapter(
getActivity(),
R.layout.item,
null,
new String[] {"Name"},
new int[] {R.id.Name}
);
setListAdapter(adapter);(光标在onStart中设置/交换)。
我想让用户能够切换选择模式(该模式将用于仅显示选中的项目)。有点像一个短信应用程序,当你把信息放在“批处理模式”,允许你选择多个对话,可以删除,保存等。我是设置一个新的适配器或只是改变现有的属性?无论如何,ListView都需要重新绘制以反映更改。
发布于 2011-12-14 22:53:23
这是一个令人惊讶的简单答案。问题是在onCreateView膨胀List之前,我在onCreate中调用了这个方法。简单地说,这只是为了测试功能。一旦我把它移到onStart,然后移到它在FragmentActivity中的预定位置,下面的操作就可以正常工作了:
public void toggleAdapter()
{
ListView listView = getListView();
if (listView.getChoiceMode() == ListView.CHOICE_MODE_MULTIPLE)
{
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
adapter = new SimpleCursorAdapter(
getActivity(),
R.layout.item,
dashboardCursor,
new String[] {"Name"},
new int[] {R.id.Name}
);
}
else
{
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
adapter = new SimpleCursorAdapter(
getActivity(),
R.layout.dashboard_list_item_multiple_choice,
dashboardCursor,
new String[] {"Name"},
new int[] {R.id.Name}
);
}
listView.setAdapter(adapter);
}发布于 2011-12-14 18:14:55
你必须做两件事。
上的样式元素
发布于 2013-06-20 03:02:46
就像Vikram Bodicherla回答。
setChoiceMode在ListView上启用MULTI_SELECT。android:background="?android:attr/activatedBackgroundIndicator"
https://stackoverflow.com/questions/8502654
复制相似问题