突出显示 ListView 选定的行
我有一张专辑列表(几百张)。 当我触摸所选专辑时,我想为用户提供播放整个专辑或移动到其曲目 ListView 的选择。没问题。 但是,在触摸 albumListView 中的专辑后,我希望该行保持突出显示,以便用户知道他们单击了哪个项目,然后可以移动到“确定”或“播放”。如何突出显示 ListView 中的一行?
I have a list of albums (several hundred).
When I touch the selected album I want to offer the user a choice of playing the whole album, or moving to its track ListView. No problem.
However, after touching the ablum in albumListView I want the row to remain highlighted so the user knows which item they have clicked on, and can then move to OK or PLAY. How do I highlight a row in ListView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
其他解决方案(主要在 XML 中)
1) 将
ListView
的choiceMode
设置为singleChoice
2) 列表项必须是 Checkable 查看和使用颜色状态列表 作为背景
例如:album_item.xml
3) 背景颜色状态 (
list_selector.xml
) 定义突出显示颜色Other solution (mostly in XML)
1) set the
choiceMode
of theListView
tosingleChoice
2) Items of the list must be a Checkable View and use a Color State List as Background
eg: album_item.xml
3) the background Color State (
list_selector.xml
) defines the highlight color引用自 http://android-developers.blogspot.de/2008/12 /touch-mode.html
Quoted from http://android-developers.blogspot.de/2008/12/touch-mode.html
Maaalte 是正确的,在触摸模式下列表不会显示当前项目的焦点突出显示,因为触摸模式下没有当前项目。不过,当您与 UI 的其他部分交互时,像您所描述的那样的有状态选择仍然存在是另一回事。
您可以使用 CHOICE_MODE_SINGLE 来表达列表中的有状态选择。它将把选择视为选中状态。只要您的适配器返回的视图实现了 Checkable 接口,您就可以按照您的选择显示选定的状态。 (在项目视图上设置背景或其他。它实际上不需要显示复选框小部件。)然后,您可以使用 ListView 的项目选中方法来操作或确定选择。
Maaalte is correct, in touch mode lists do not show the focus highlight on a current item since there is no current item in touch mode. Stateful selection like you're describing that persists while you interact with other parts of the UI is another matter though.
You can use CHOICE_MODE_SINGLE to express a stateful selection in a list. It will treat selection as a checked state. As long as the view that your adapter returns implements the Checkable interface, you can display the selected state however you choose. (Setting a background on the item view or otherwise. It does not actually need to show a check box widget.) You can then use the item checked methods of ListView to manipulate or determine the selection.
我通过扩展 ListView 并重写 getView() 方法解决了这个问题。我保留了“选定”状态的内部标识符,并根据它更改了项目的背景,如下所示:
我的博客文章中有一个示例程序:
http://udinic.wordpress.com/2011/07/01/selectablelistview-make -选择工作/
I solved this by extending the ListView and overriding the getView() method. I kept an internal identifier for the "selected" state and changed the background of the item according to it, like this:
I have an example program on my blog post:
http://udinic.wordpress.com/2011/07/01/selectablelistview-make-selection-work/
这是我的解决方案
Listview:
为 list_row 编写一个选择器
确保您有 state_activated=true 的项目
然后将此选择器添加为 list_row 背景
Here is my solution
Listview:
Write a selector for list_row as
Make sure you have item for state_activated=true
Then add this selector as your list_row background
添加
android:background="?activatedBackgroundIndicator
到列表项的主布局,类似于
android.R.layout.simple_list_item_activated_2.xml
add
android:background="?activatedBackgroundIndicator
to list item's main layout similar to
android.R.layout.simple_list_item_activated_2.xml
使用单选模式实现列表:
android:choiceMode=singleChoice
正如默认的
android.R.layout.simple_list_item_single_choice
使用RadioButton
来表示选定的选项,您可以在列表项布局中实现自定义突出显示状态或类似的状态以响应选项更改。如有必要,您的应用程序可以使用getCheckedItemPosition()
等方法来确定用户当前选择的项目。希望有帮助!
Implement your list with single choice mode:
android:choiceMode=singleChoice
Just as the default
android.R.layout.simple_list_item_single_choice
uses aRadioButton
to denote the selected choice, you can implement a custom highlight state or similar in your list item layout to respond to the choice changes. Methods likegetCheckedItemPosition()
can be used by your application to determine which item the user currently has chosen, if necessary.Hope that Helps!
我知道这个问题上次活跃已经有两年了,但以防万一其他人将来需要它。这是我的 ListView xml 代码
您想要更改
android:listSelector
值。I know that it's two years since this question was last active, but just in case someone else needs it in the future. Here's my ListView xml code
You want to change the
android:listSelector
value.以下解决方案可用于突出显示一个或多个列表项。起点:有一个 ListFragment 通过 ArrayAdapter 实现显示 MyEntity 列表。如果选择了项目,则作为项目布局一部分的 TextView 的颜色必须更改为灰色。应该可以选择多个项目。
ListFragment 必须提供将在 onListItemClicked() 中调用的 OnItemSelectListener(YourEntity item)。
ArrayAdapter 拥有一个包含所选项目的关键字段的列表,并提供管理器方法 addSelection(MyEntity) 和clearAllSelections()。重写方法 getView() 读取关键字段列表,如果实际位置在其中,则将特定 TextView 颜色更改为灰色,否则更改为透明。
在拥有活动的 onCreate() 中,必须实现监听器来管理 ArrayAdapter:调用 addSelection() 和 notificationDataSetChanged()。
Following solution can be used for highlighting one ore many list items. Starting point: There is a ListFragment to show a List of MyEntity via an ArrayAdapter implementation. The color of an TextView as part of the item layout has to change to grey, if the item is selected. Multiple items should be selectable.
The ListFragment must provide an OnItemSelectListener(YourEntity item) that will be called in onListItemClicked().
The ArrayAdapter owns a List for the key fields for the selected items and provides the manager methods addSelection(MyEntity) and clearAllSelections(). The overriden method getView() reads the key field list and, if the actual position is in there, changes the particular TextView color to grey, otherwise to transparent.
In onCreate() of the owning activity the listeners has to be implemented to manage the ArrayAdapter: calling addSelection() and notifyDataSetChanged().
我遇到了同样的问题,我通过在 ListView 代码中粘贴以下代码解决了
它 xml 文件如下所示
I had the same problem, I solved it by pasting the following code in the ListView code
The xml file looks as the following
就我而言,当使用 simple_list_item_2 时,唯一的解决方案是重写适配器的 getView() 方法,然后手动更改背景颜色:
记住每当更改所选项目时都要调用 listview.invalidateViews() 。
In my case, when using simple_list_item_2, the only solution was to override the getView() method of the adapter, and just change the background color manually:
Remember to call listview.invalidateViews() whenever you change the selected item.
这就是我所做的:) - 在头撞墙之后。
对于适配器:
对于 row_layout.xml:
要设置要突出显示的默认位置,只需对 listView 对象执行以下操作:
radioTitleList.setItemChecked(defaultPosition, true);
如果您想在单击列表时突出显示:
在自定义列表适配器的 getView() 中,在返回视图之前添加以下内容:
Here's what I did :) - after major head whacking against wall.
For the adapter:
For the row_layout.xml:
To set a default position to be highlighted just do this to your listView object:
radioTitleList.setItemChecked(defaultPosition, true);
If you want highlighting when you click on the list:
In your getView() in your custom list adapter add this before returning the view: