Android 使 ListView 中的项目可选

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11064115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:52:03  来源:igfitidea点击:

Make items within ListView selectable

androidandroid-listviewtextviewandroid-actionbar

提问by Boardy

I am working on an android application and I am trying to follow the guidelines for the Ice Cream Sandwich UI.

我正在开发一个 android 应用程序,我正在尝试遵循 Ice Cream Sandwich UI 的指南。

I have a ListActivity which contains a ListView and within the ListView I am have a TextView for each item within an ArrayAdapter. When I want to be able to do, is once the user long clicks on an item within the ListView and Context Activity Bar is shown and the item that was long clicked is now in a selected state. From there the user can then click on multiple items within the ListView making them all selected and then the user can press a button within the ActionBar to perform an action on all selected items within the ListView.

我有一个 ListActivity,它包含一个 ListView,在 ListView 中,我为 ArrayAdapter 中的每个项目都有一个 TextView。当我希望能够做到时,一旦用户长单击 ListView 中的一个项目并显示 Context Activity Bar,并且长按的项目现在处于选定状态。从那里,用户可以单击 ListView 中的多个项目,使它们全部被选中,然后用户可以按下 ActionBar 中的按钮对 ListView 中的所有选定项目执行操作。

I've got it triggering to show the context activity bar when the item is long clicked but I have no idea how I can make the item appear selected and allow the user to select/deselect the items within the ListView.

当长按项目时,我已经触发以显示上下文活动栏,但我不知道如何使项目显示为选中状态并允许用户选择/取消选择 ListView 中的项目。

Thanks for your help

谢谢你的帮助

回答by Jug6ernaut

Do as is described here.

按照此处的说明进行操作。

How to highlight selected item in ListView?

如何突出显示ListView中的选定项目?

Basically you need to set the multiselect listener and then selection type. Do what you need from the actionmode callbacks

基本上你需要设置多选侦听器,然后选择类型。从 actionmode 回调中执行您需要的操作