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

Android 如何在ListView中突出显示所选项目?,android,android-listview,Android,Android Listview,我知道android在触摸模式下不会突出显示任何内容。但我正在做一些类似于gmail应用程序的事情,在这个应用程序中,你可以从左侧选择内容,并在活动的右侧显示详细信息(不知道谷歌是如何做到这一点的) 因此,我必须突出显示左侧列表视图中选择的内容。我发现了一些类似的问题,解决方案基本上是: 1.覆盖适配器的getView方法和所选位置的setBackground 2.在视图的背景上单击并清除它以进行花药选择 但由于一种奇怪的行为,它们中没有一个对我有效:当我点击一个项目并高亮显示它时,它后面的第五

我知道android在触摸模式下不会突出显示任何内容。但我正在做一些类似于gmail应用程序的事情,在这个应用程序中,你可以从左侧选择内容,并在活动的右侧显示详细信息(不知道谷歌是如何做到这一点的)

因此,我必须突出显示左侧列表视图中选择的内容。我发现了一些类似的问题,解决方案基本上是:

1.覆盖适配器的getView方法和所选位置的setBackground

2.在视图的背景上单击并清除它以进行花药选择

但由于一种奇怪的行为,它们中没有一个对我有效:当我点击一个项目并高亮显示它时,它后面的第五个项目也高亮显示,等等,当我向下滚动列表时


有什么建议吗?谢谢

高亮显示多个项目的原因可能是:重用整个视图,或者将这两个视图的背景设置为同一个可绘制实例。如果在屏幕上有两次相同的drawable,那么发生在第一个drawable上的所有事件都会发生在其他所有事件上,因为该逻辑是在drawable自身的实例中实现的

若要解决此问题,请执行以下操作之一:不要对多行重复使用视图,或者不要对多行重复使用可绘图项(每次创建一个新的可绘图项)

我知道这听起来是资源密集型的,但除非你有更好的解决方案,否则这是最简单的解决方案

使用listView.setChoiceMode(int choiceMode)

参数

从类android.widget.AbsListView中选择模式选项_MODE_NONE、选项_MODE_SINGLE或选项_MODE_MULTIPLE

您还需要添加MultichoiceModelListener,您可以选择\u MODE\u SINGLE

参考下面的示例


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

这是用于自定义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);
}
 <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"/>
@覆盖
public void onItemClick(AdapterView父视图、视图视图、整型位置、长arg3)
{
对于(int a=0;a
//@drawable/list\u选择器

 <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上

@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);
}
 <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>

将此用作列表选择器:

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

选择首选的高光颜色

谢谢,伙计。。我现在试图看看代码在哪里重用视图,因为我没有编写它。顺便问一下,有没有可能让选择器停留在顶部作为突出显示,而不是改变背景?如果可行的话会更有意义。非常感谢!通过添加一个MultiChoiceModelListener并在xml文件中设置适配器的背景,它工作了!但我想知道为什么Android只有在选中项目时才使用背景色。如此自动