在 Android 的列表视图中仅突出显示所选项目
我有一个列表视图contactslist
。我编写了用于突出显示 ListView
中所选项目的代码。它正在发挥作用。当我单击一个项目时,它会突出显示该项目,但问题是如果我单击另一项目,它也会突出显示该项目。我只想突出显示所选项目。当我单击另一个项目时,之前的选择应该消失。
arg1.setBackgroundResource(R.drawable.highlighter);
这是点击侦听器中用于突出显示所选项目的代码。我该如何解决这个问题?
我正在设置适配器中行的背景:
public int[] colors = new int[]{0xFFedf5ff, 0xFFFFFFFF};
public int colorPos;
[...]
colorPos = position % colors.length;
row.setBackgroundColor(colors[colorPos]);
I have a list view contactslist
. I wrote the code for highlighting the selected item in the ListView
. It is working. When I click on one item it is highlighting that item, but the problem is if I click on another item it is highlighting that one too. I want to highlight the selected item only. The previous selection should go away when I click on another item.
arg1.setBackgroundResource(R.drawable.highlighter);
This is the code in the click listener using to highlight the selected item. How can I fix this?
I'm setting the background of the rows in the adapter:
public int[] colors = new int[]{0xFFedf5ff, 0xFFFFFFFF};
public int colorPos;
[...]
colorPos = position % colors.length;
row.setBackgroundColor(colors[colorPos]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ListViews
默认情况下没有设置choiceMode
(它设置为none
),因此当前选择不会以视觉方式指示。要更改此设置,您只需将
ListView
的choiceMode
属性设置为singleChoice
。如果您希望为列表中的所选项目设置自定义背景,还应该设置
listSelector
属性。在那里您不仅可以指定颜色,还可以指定可绘制对象(图像、图层/状态可绘制对象)。如果您不直接使用
ListView
,而是使用ListActivity
,则需要从代码中设置这些属性,因此您应该扩展 Activity 的onCreate 方法包含以下几行:
因此,如果您使用单击侦听器来更改所选行的背景,请从代码中删除它,并使用上面的正确方法。
回复更新
如果您通过 getView 方法设置背景,则不要使用静态颜色,而是将可绘制的状态列表应用于行背景,并将重复父状态设置为 true。这样它将根据项目的当前状态更改其显示:正常、聚焦、按下等。
ListViews
by default don't have achoiceMode
set (it's set tonone
), so the current selection is not indicated visually.To change this, you just need to set the
choiceMode
attribute of yourListView
tosingleChoice
.If you'd like custom background for the selected items in your list, you should also set the
listSelector
attribute. There you can specify not only colors, but drawables (images, layer-/state-drawables).If you don't use a
ListView
directly, but aListActivity
, then these attributes need to be set from code, so you should extend your activity'sonCreate
method with these lines:So if you were using a click listener to change the background of the selected row, remove that from your code, and use the proper method from above.
Reply to the update
If you set the background from your getView method, instead of using a static color, apply a state list drawable to the row background with duplicateParentState set to true. This way it will change its display based on the current state of the item: normal, focused, pressed, etc.
在listview xml中添加“singleChoice”模式
在列表项布局中添加
示例
In the listview xml add the "singleChoice" mode
In the list item layout add
example
更好的方法是
在你的主题中,
@drawable/list_selector
list_selector.xml :
然后为 list_row.xml android:background="?android:attr/activatedBackgroundIndicator" 的根设置背景
A better way is
in your theme,
@drawable/list_selector
list_selector.xml :
then set background for root of your list_row.xml android:background="?android:attr/activatedBackgroundIndicator"
尝试一下
onListItemClick
它会突出显示所选内容。
Try this at
onListItemClick
It highlights the selection.