如何突出显示ListView中的选定项目? [英] How to highlight selected item in ListView?

查看:22
本文介绍了如何突出显示ListView中的选定项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 android 不会在 TouchMode 中突出显示任何内容.但是我正在做一些类似于 gmail 应用程序的事情,在该应用程序中,您从左侧选择内容并在活动的右侧显示详细信息(想知道 Google 是如何做到的).

I know that android doesn't highlight anything in TouchMode. But I am doing something similar to the gmail application in which you select stuff from the left side and show details on the right side of the activity(wonder how Google did that).

所以故事是我必须突出显示在左侧 ListView 中选择的内容.我发现了一些类似的问题,解决方案基本上是:

So the story is I have to highlight what's been selected on the left side ListView. I've found some similar questions and the solutions are basically:

1.覆盖适配器的getView方法和选定位置的setBackground

1.override the adapter's getView method and setBackground for selected position

2.setBackground 视图 onItemClick 并清除它以供选择

2.setBackground of the view onItemClick and clear it for anther selection

但由于奇怪的行为,它们都不适合我:当我点击一个项目并突出显示它时,它之后的第五个项目也被突出显示,依此类推,当我向下滚动列表时.

But none of them worked for me due to a weird behaviour: As I click on one item and highlight it, the fifth item after it is highlighted as well, and so on so forth as I scroll down the list.

有什么建议吗?谢谢!

推荐答案

使用 listView.setChoiceMode(int choiceMode);

Use listView.setChoiceMode(int choiceMode);

参数

choiceMode 来自类 android.widget.AbsListView 的 CHOICE_MODE_NONE、CHOICE_MODE_SINGLE 或 CHOICE_MODE_MULTIPLE 之一

choiceMode One of CHOICE_MODE_NONE, CHOICE_MODE_SINGLE, or CHOICE_MODE_MULTIPLE from class android.widget.AbsListView

http://developer.android.com/reference/android/widget/AbsListView.html#setChoiceMode(int)

还需要添加MultiChoiceModeListener,可以有CHOICE_MODE_SINGLE

You also need to add MultiChoiceModeListener, you can have CHOICE_MODE_SINGLE

(android.widget.AbsListView.MultiChoiceModeListener)

参考下面的示例

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List16.html

这篇关于如何突出显示ListView中的选定项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆