通过代码使 ListView 项目发光
这是我的问题:我有一个 Activity
,其中包含一个列出了不同位置的 ListView
,以及一个带有这些位置标记的 MapView
。
我现在想要的是,当单击其中一个 MapView 标记时,选择相应的 ListView 项目并发光(就像单击它一样)。
我找到了方法 ListView.setSelection(int)
和 ListView.requestChildFocus(View, View)
。第一个几乎完成了我想要的操作(如果我有一个很长的列表,它会遍历它直到该项目可见),但它缺乏发光效果来明确显示该项目。关于第二个,我不确定第二个参数的用途(它是活动中先前聚焦的视图?)。
那么有没有办法让物品发光呢?就像您使用物理键盘选择它一样。
谢谢。
编辑:附属问题,单击 ListView 项目时是否可以获取用作背景的标准可绘制对象?可以在 R.attr 中找到它,例如 listPreferredItemHeight
或其他内容吗?
Here's my problem : I have an Activity
that includes a ListView
with different locations listed, and a MapView
with markers to these locations.
What I want right now is, when one of the MapView
markers is clicked, to have the respective ListView
item to be selected and to glow (as if it was clicked).
I found the methods ListView.setSelection(int)
and ListView.requestChildFocus(View, View)
. The first one does almost what I want (if I have a long list, it goes through it until the item is visible) but it lacks a glow effect to explicitly show the item. About the second, I'm not sure what the second parameter is for (it's the previously focused view in the activity ?).
So, is there a way to have the item glow ? Like when you select it using a physical keyboard.
Thanks.
EDIT : subsidiary question, is it possible to get the standard drawable used as background when a ListView item is clicked ? Can it be found in R.attr like listPreferredItemHeight
or something ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不想选择项目,而是选择它们。这允许您使用 StateListDrawable 来更改项目的外观,因为项目在触摸模式下不会保持选中状态。您必须将
ListView
置于单选模式,使列表视图项目可检查,并使用StateListDrawable
更改已检查项目的外观。要将 ListView 置于单选模式:
然后,当您想要突出显示
ListView
中的某个项目时,您可以执行以下操作:我相信行布局的根项目必须实现
Checkable
>,您应该能够在线找到CheckableRelativeLayout
或CheckableLinearLayout
的示例。这会使您的项目“选中”,但您仍然需要使其发光,因此在行的布局中,您必须使用 StateListDrawable 来在选中时更改其外观,方法是将其添加到布局中:
并创建像这样的
color\list_view_item_selector.xml
文件:Instead of making the items selected, you want to make them checked. This allows you to use a
StateListDrawable
to change the appearance of the item, since items don't stay selected when they are in touch mode. You have to put yourListView
into single choice mode, make your listview item checkable, and use theStateListDrawable
to change the appearance for checked items.To put your ListView in single choice mode:
Then when you want to highlight an item in your
ListView
, you do this:I believe the root item of your row's layout must implement
Checkable
, you should be able to find example ofCheckableRelativeLayout
orCheckableLinearLayout
online.That makes your item "checked" but you still need to make it glow, so in your row's layout you must use a
StateListDrawable
to change its appearance when it is checked by adding this to the layout:and creating a
color\list_view_item_selector.xml
file like this: