ListView 保持选中状态?
我有一个充满项目的列表视图,在用户选择一个项目后它会亮起,然后恢复正常。有没有办法让用户在 ListView 中选择一个项目时它保持选中状态并突出显示?
I have a list view full of items, after the users selects an item it lights up, and then it goes back to normal. Is there a way to make it so that when the user selects an item in my ListView it stays selected, and highlighted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
显然,“消失的选择”是有意设计的。这就是所谓的“触摸模式”。我通读了该文件,但仍然不知道为什么他们认为这是一个好主意。我的猜测是,由于 Android 最初是为小屏幕设备设计的,因此他们希望您用一个列表填充屏幕,然后当用户单击某个项目时,移动到另一个屏幕上的新列表。因此,用户不会意识到 Android 丢失了所选项目的跟踪。
但是,例如,如果您希望用户选择一个项目,然后在同一屏幕上显示有关该项目的信息,则这种行为非常烦人。如果选择消失,用户如何知道他们点击了什么(当然假设用户的注意力集中度像金鱼一样)?
一种可能的解决方案是将所有列表项更改为单选按钮。我不太喜欢这个解决方案,因为它浪费了屏幕空间。我宁愿只使用背景颜色来显示选择了哪个项目。到目前为止,我已经看到了一种解决方案,但它并不十分完整或通用。所以这是我的解决方案:
1. 在 XML 布局文件中,
转到 ListView 元素和以下属性:
android:choiceMode="singleChoice"
。我不完全确定它的作用(它本身不允许用户选择任何内容),但如果没有此属性,下面的代码将无法工作。2. 定义以下类
它用于跟踪所选项目,并且还允许您模拟 Java 中的引用传递:
3. 将以下代码放在某处
我假设您将其放在 Activity 中,但是它实际上可以放在任何类中:
此代码做了两件事:它将列表项(例如
List
)附加到 ListView,并覆盖ArrayAdapter.getView()
code> 包含一些更改所选项目背景的代码。4. 使用该代码设置您的列表
例如:
仅此而已!上面假设您想要单一选择。我想,通过对 getView() 进行一些小的修改,您也可以支持多重选择,但您可能应该使用复选框。
警告:此解决方案需要进一步开发。如果用户使用箭头键或按钮选择某个项目,则从 IntHolder 的角度将不会选择该项目。如果用户按下未标记的按钮(该按钮的名称是什么?“Enter”?),那么该项目将成为“正式”选择,但接下来你会遇到另一个问题,因为如果用户再次使用箭头键,它看起来会有点像就像选择了两个项目一样。如果您弄清楚如何使 IntHolder 中的“内部选择”与“键盘选择”或任何名称保持同步,请发表评论。它叫什么?
Apparently the "disappearing selection" is by design; it's something called "touch mode". I read through that document and still I have no idea why they thought it was a good idea. My guess is that, since Android was originally designed for small-screen devices, they expected that you would fill the screen with a list and then, when the user clicks an item, move to a new list on a different screen. Thus, the user wouldn't be aware that Android lost track of the selected item.
But this behavior is quite annoying if, for example, you want the user to select an item and then show information about that item on the same screen. If the selection disappears, how is the user supposed to know what they clicked (assuming of course that users have the attention span of a goldfish)?
One possible solution is to change all the list items into radio buttons. I don't really like that solution because it wastes screen real estate. I'd rather just use the background color to show which item is selected. I have seen one solution so far but it is not quite complete or general. So here's my solution:
1. In your XML layout file
Go to your ListView element and the following attribute:
android:choiceMode="singleChoice"
. I'm not entirely sure what this does (by itself, it doesn't allow the user to select anything) but without this attribute, the code below doesn't work.2. Define the following class
It is used to keep track of the selected item, and also allows you to simulate pass-by-reference in Java:
3. Put the following code somewhere
I'll assume you put it in your Activity, but it could go in any class really:
This code does two things: it attaches your list items (e.g.
List<String>
) to your ListView, and it overridesArrayAdapter.getView()
with some code that changes the background of the selected item.4. Use that code to set up your list
For example:
That's all! The above assumes you want single selection. With some small modifications to getView(), you could support multi-selection too, I guess, but you should probably use checkboxes instead.
Warning: this solution needs further development. If the user uses arrow keys or buttons to select an item, that item will not be selected from the IntHolder's perspective. If the user presses the unlabeled button (what's the name of that button? "Enter"?) then the item will become "officially" selected but then you have another problem because if the user uses the arrow keys again, it will sort of look like two items are selected. Leave a comment if you figure out how to keep the "internal selection" in the IntHolder synchronized with the "keyboard selection" or whatever it's called. What is it called, anyway?
ListView中有一个属性叫做listSelector:
http://developer.android.com/reference/android/ widget/AbsListView.html#attr_android:listSelector
编辑 在Stan之后comment
要确保
ListView
保持选中状态,您应该① 通过 xml 或以编程方式设置视图的属性
choiceMode
。② 使用适配器,该适配器使用实现
Checkable
< 的视图/a> 界面,如CheckedTextView
(simple_list_item_single_choice
布局)。文件
TestActivity.java
There is an attribute in ListView called listSelector:
http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:listSelector
EDIT after Stan comment
To ensure that a
ListView
stays selected, you should① Set the view's attribute
choiceMode
via xml or programmatically.② Use an adapter that uses views which implement
Checkable
interface, likeCheckedTextView
(insidesimple_list_item_single_choice
layout).File
TestActivity.java
这里有一个比 Qwertie 更简单的解决方案:
不要依赖给定的选择机制。自己做吧。
此外,此方法仅处理点击,如果用户使用箭头键,则该方法将不起作用。
免责声明:触摸后取消突出显示无法可靠地工作。
触摸部分的积分请转到 ozik.dev:
仅使用 OnTouchListener 从 ListView 获取项目
Here a simpler solution than Qwertie's:
Do not rely on given selection mechanism. Do it yourself.
Also this method only deals with clicks and will not work if the user uses the arrow keys.
Disclaimer: De-highlighting after touch does not work reliably.
Credits for the touching part go to ozik.dev:
Get Item from ListView only with OnTouchListener
只需将其添加到您的列表视图布局中即可
just add this to your listview layout
使用 Selector.XML 文件和以下代码:
Use a Selector.XML File and this code:
只需将其添加到您的 ListView 中:
Just add this to your ListView:
这个答案有效,试试这个
This answer is working try this one