如何在ListView中突出显示所选项目?

11

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

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

1.覆盖适配器的getView方法并为选定位置设置背景

2.在onItemClick中设置视图的背景并清除另一个选择

但由于一种奇怪的行为,它们都对我不起作用:当我单击一个项目并将其突出显示时,之后的第五个项目也会被突出显示,随着我向下滚动列表,这种现象还会继续发生。

有什么建议吗?谢谢!

6个回答

23

使用listView.setChoiceMode(int choiceMode)方法:

参数:

choiceMode 是android.widget.AbsListView类中CHOICE_MODE_NONE、CHOICE_MODE_SINGLE或CHOICE_MODE_MULTIPLE之一。

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

还需要添加MultiChoiceModeListener,可以使用CHOICE_MODE_SINGLE。

(android.widget.AbsListView.MultiChoiceModeListener)

参考下面的示例:

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


非常感谢!通过添加MultiChoiceModeListener并在xml文件中设置适配器的背景,它已经可以工作了!但是我想知道为什么Android只会在选择项目时才使用背景颜色..所以这就自动完成了。 - Han

16

这是为自定义的ListActivity或ListFragment准备的

在ListView中突出显示所选项目

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
{

    for(int a = 0; a < parent.getChildCount(); a++)
    {
        parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
    }

    view.setBackgroundColor(Color.GREEN);
}

2

////@drawable/list_selector

 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/list_item_bg_normal"
 android:state_pressed="false" android:state_selected="false" android:state_activated="false"/>

<item android:drawable="@drawable/list_item_touch"
 android:state_pressed="true"/>

 <item android:drawable="@drawable/list_item_touch"
 android:state_pressed="false" android:state_selected="true"/>

 <item android:drawable="@drawable/list_item_bg_pressed"
 android:state_activated="true"/>

 </selector>

////////////////////还有关于ListView的内容

 <ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"
    android:divider="#060606"/>

///////////////////////列表视图项

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@drawable/list_selector">

  <ImageView
  android:id="@+id/icon"
  android:layout_width="35dp"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_marginLeft="12dp"
  android:layout_marginRight="12dp"
  android:contentDescription="@string/desc_list_item_icon"
  android:src="@drawable/ic_home"
  android:layout_centerVertical="true" />

  </RelativeLayout>

1
你之所以会同时高亮多个项目,可能是因为你要么:重用整个视图,要么将这两个视图的背景设置为相同的Drawable实例。如果你在屏幕上有两个相同的drawable,那么发生在第一个drawable上的所有事件都会发生在其他所有drawable上,因为这个逻辑是在Drawable实例本身中实现的。
为了解决这个问题,可以选择:不要为多行重复使用Views,或者不要为多行重复使用Drawables(每次创建一个新的)。
我知道这听起来很耗资源,而且确实如此,但除非你已经想出了更好的解决方案,否则这是最简单的修复方法。

谢谢,我现在正在尝试查看代码重用视图的位置,因为我没有编写它。顺便问一下,是否可以让选择器保持在顶部作为高亮显示,而不是更改背景?如果可行的话,这样做更有意义。 - Han

0
由于列表视图中的项模仿顶部的项,它们也会模仿它们的背景。您必须在getView()函数中设置每个项的背景。在getView()中的每个项中,您必须为选定的项和未选定的项设置背景。

0

使用此作为列表选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/list_bg" />
</selector>

还有这个list_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/list_bg_color" />
</shape>

选择首选的高亮颜色


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接