在android的listview中突出显示所选项目

Posted

技术标签:

【中文标题】在android的listview中突出显示所选项目【英文标题】:highlighting the selected item in the listview in android 【发布时间】:2011-08-16 18:12:55 【问题描述】:

我有 1 个列表视图 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]);

【问题讨论】:

如果答案可以接受,我可以接受 dat。但如果答案对我没有帮助,我该如何接受? 如果您对答案不满意,请忘记我之前评论的提升部分。但更多信息将真正帮助我们找出您的问题根源,以及如何帮助您。谢谢! 我正在使用相对布局。我正在使用适配器为列表视图设置背景,我的逻辑是,如果 rawnum 是奇数,则给出背景颜色,如果它是偶数,则给出另一种背景颜色。下面是逻辑实现。public int[] colors = new int[] 0xFFedf5ff, 0xFFFFFFFF;公共 int colorPos; colorPos = 位置 % 颜色。长度; row.setBackgroundColor(colors[colorPos]); 请从现在开始编辑您的问题以共享代码 sn-ps,而不是将它们粘贴到 cmets 中,因为在这里它们不太可读。如果您提供处理所选背景的代码,这也会很有帮助。谢谢! 谢谢....但还没有得到解决方案。我被困在这个问题上。请帮助我 【参考方案1】:

ListViews 默认情况下没有choiceMode 设置(设置为none),因此当前选择不会在视觉上显示。

要更改此设置,您只需将ListViewchoiceMode 属性设置为singleChoice。 如果您想为列表中的选定项目自定义背景,您还应该设置listSelector 属性。在那里,您不仅可以指定颜色,还可以指定可绘制对象(图像、图层/状态可绘制对象)。

<ListView android:id="@+id/my_list"
        android:choiceMode="singleChoice" 
        android:listSelector="@android:color/darker_gray" />

如果您不直接使用ListView,而是使用ListActivity,则需要从代码中设置这些属性,因此您应该使用这些行扩展您的活动的onCreate 方法:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setSelector(android.R.color.darker_gray);

因此,如果您使用点击侦听器来更改所选行的背景,请将其从您的代码中删除,然后使用上面的正确方法。

回复更新

如果您从 getView 方法设置背景,而不是使用静态颜色,请将可绘制的状态列表应用于行背景,并将 duplicateParentState 设置为 true。这样,它将根据项目的当前状态更改其显示:正常、聚焦、按下等。

【讨论】:

我尝试了这段代码,但是如果我将列表视图中的choice_mode_single den 设为突出显示的第一个项目,但如果我选择另一个项目,则它不会突出显示。 是否有任何方法或属性可以在列表视图中获取先前选择的项目? 您是否删除了使用 onitemclick 侦听器设置背景的代码? 我正在使用android.support.v4.app.ListFragment。在 onCreate() 中添加行 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 会给我异常:java.lang.RuntimeException: Unable to start activity ComponentInfo...: android.view.InflateException: Binary XML file line #90: Error inflating class fragment 我过去尝试过很多组合,但最近又遇到了这个问题。我想知道它是否特定于设备,我的 2.3 设备与 Youngjae 遇到的问题相同。【参考方案2】:

在listview xml中添加“singleChoice”模式

<ListView
    android:layout_
    android:layout_
    android:choiceMode="singleChoice"
    (...) >
</ListView>

在列表项布局中添加

android:background="?android:attr/activatedBackgroundIndicator

例子

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_
   android:layout_
   android:orientation="horizontal"
   android:background="?android:attr/activatedBackgroundIndicator">

     <!-- your item content-->

</LinearLayout> 

【讨论】:

为什么这不是谷歌上出现的第一件事!?我已经尝试了几个小时才能让它发挥作用。我在带有android:listSelector="@drawable/theSelector" 设置的可绘制选择器中尝试了各种状态和状态组合(选择、激活、聚焦等),但是当它工作时它有问题(当我滚动时,选择指示器会卡住列表,有时甚至停留在两个项目之间)。这终于使它正常工作。谢谢。 我喜欢单行...非常感谢!【参考方案3】:

更好的方法是 在你的主题中, @drawable/list_selector

list_selector.xml:

<!-- <item android:drawable="@color/android:transparent"  android:state_selected="true" /> -->
<item android:drawable="@color/list_bg" android:state_selected="true"/>
<item android:drawable="@color/list_bg" android:state_activated="true"/>
<item android:drawable="@color/transparent"/>

然后为您的 list_row.xml android:background="?android:attr/activatedBackgroundIndicator" 的根目录设置背景​​

【讨论】:

【参考方案4】:

onListItemClick试试这个

view.getFocusables(POSITION);
view.setSelected(true);

它突出显示选择。

【讨论】:

以上是关于在android的listview中突出显示所选项目的主要内容,如果未能解决你的问题,请参考以下文章

在 ListView 中默认选择一行并突出显示所选行 Android

选择其他按钮时,ListView 突出显示所选项目颜色不会保持不变

Android ListView 所选项目保持突出显示

如何在Android中的ListView中突出显示行?

如何更改 UWP 中选定 ListView 项的突出显示颜色(Windows 10)

Android ListView 突出显示会导致奇怪的行为