通过代码使 ListView 项目发光

发布于 2024-10-28 05:34:54 字数 549 浏览 7 评论 0原文

这是我的问题:我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中的蝴蝶 2024-11-04 05:34:54

您不想选择项目,而是选择它们。这允许您使用 StateListDrawable 来更改项目的外观,因为项目在触摸模式下不会保持选中状态。您必须将 ListView 置于单选模式,使列表视图项目可检查,并使用 StateListDrawable 更改已检查项目的外观。

要将 ListView 置于单选模式:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

然后,当您想要突出显示 ListView 中的某个项目时,您可以执行以下操作:

getListView().setItemChecked(index, true);

我相信行布局的根项目必须实现 Checkable >,您应该能够在线找到 CheckableRelativeLayoutCheckableLinearLayout 的示例。

这会使您的项目“选中”,但您仍然需要使其发光,因此在行的布局中,您必须使用 StateListDrawable 来在选中时更改其外观,方法是将其添加到布局中:

android:background="@color/list_view_item_selector"

并创建像这样的 color\list_view_item_selector.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@color/selected_item" />
    <item android:drawable="@android:color/transparent" />
</selector>

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 your ListView into single choice mode, make your listview item checkable, and use the StateListDrawable to change the appearance for checked items.

To put your ListView in single choice mode:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Then when you want to highlight an item in your ListView, you do this:

getListView().setItemChecked(index, true);

I believe the root item of your row's layout must implement Checkable, you should be able to find example of CheckableRelativeLayout or CheckableLinearLayout 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:

android:background="@color/list_view_item_selector"

and creating a color\list_view_item_selector.xml file like this:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@color/selected_item" />
    <item android:drawable="@android:color/transparent" />
</selector>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文