Android:当ListView打开时将项目设置为选中状态?
一个 Activity 有一个 Button 和一个 ListView。 最初,只有按钮可见。当按下按钮时,将显示 ListView。 显示时,我是否可以将一项特定项目显示为选定/聚焦?
一个用例可能是假设它是语言设置列表,并且当列表打开时,当前选择的语言必须显示为突出显示。
如果我知道该项目的索引,如何将其设置为重点显示?
An activity has a Button and a ListView.
Initially, only the Button is visible. When the button is pressed, the ListView is displayed.
When displayed, is it possible for me to show one particular item as selected/focussed?
A use case could be that suppose it is a list of language settings and when the list opens, the currently selected language must be shown as highlighted.
If I know the index of the item, how to set it as focused on display?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发布我的解决方案,因为谷歌仍然不知道答案。
I post my solution, because google still doesn't know the answer.
简而言之,ListView::setSelection(intposition) 就是您所需要的。但是,根据设备是否处于触摸模式,它可能有也可能没有视觉效果(背景突出显示)。更多详情请参考Android ListView选择问题
In short, ListView::setSelection(int position) is what you need. However, depending on whether the device is in touch mode or not, it may or may not have visual effect (background highlighting). For more details, refer to Android ListView Selection Problem
如果您为
ListView
使用Adapter
,请将此代码添加到您的适配器中:在您的
Activity
中:If you use an
Adapter
for yourListView
add this code to your adapter:In your
Activity
:我正在使用适配器,并且不想设置自定义背景颜色,但在可绘制 xml 中使用 android:state_selected 。 SetSelection 对我不起作用,但也许这也是因为我需要 SetNotifyDataChanged,它表明选定状态不是持久的。
我还发现 ListView 中项目的选定状态不是持久的,因为 SetNotifyDataChanged 会导致更新 ListView 布局,从而清除所有项目。在 Adapter 的 GetView 中将项目设置为 Selected 也太快了。
最终,在列表视图的布局更改后,即触发 LayoutChange 事件时,我为所选项目的视图设置了“选定”状态(在 Java 中,它可能会附加到 ListView 的 OnLayoutChangeListener)。
为了使它变得非常简单,我将所选项目的视图存储为适配器的 SelectedItemView。
在 ListView 的 LayoutChange 事件处理程序中,我只是将适配器的 SelectedItemView.Selected 设置为 true。
这是我的 Activity 中的代码,其中我为 ListView 设置了 Adapter,并订阅了 LayoutChange(或在 Java 中附加了一个 OnLayoutChangeListener),
这是我的 Adapter 代码:
I am using an Adapter and didn't want to set custom background colors, but use the android:state_selected in drawable xml. SetSelection didn't work for me, but maybe that's also since I needed SetNotifyDataChanged which shows that the Selected State is not persistent.
I also found that the Selected state for an item in a ListView is not persistent, since SetNotifyDataChanged results in updating the ListView layout which clears them all. Setting the item to Selected in the Adapter's GetView is too soon too.
Eventually I set the Selected state for the view of the selected item after the layout of the listview has been changed, which is when LayoutChange event is being triggered (in Java it's probably attaching a to OnLayoutChangeListener of the ListView).
To make it really easy I store the view of the selected item as Adapter's SelectedItemView.
In the ListView's LayoutChange eventhandler I just set the adapter's SelectedItemView.Selected to true.
Here's the code from my Activity where I set the Adapter for the ListView and also subscribe to LayoutChange (or in Java attach an OnLayoutChangeListener)
Here's my code for the Adapter: