如何在列表视图中突出显示选定的项目?

时间:2023-01-19 11:32:33

I know that android doesn't highlight anything in TouchMode. But I am doing something similar to the gmail application in which you select stuff from the left side and show details on the right side of the activity(wonder how Google did that).

我知道android在TouchMode下没有突出显示任何东西。但是我正在做一些类似于gmail应用程序的事情,在这个应用程序中,您从左侧选择内容,并在活动的右侧显示细节(想知道谷歌是如何做到这一点的)。

So the story is I have to highlight what's been selected on the left side ListView. I've found some similar questions and the solutions are basically:

所以故事是,我必须突出显示在左边列表视图中选择的内容。我发现了一些类似的问题,解决办法基本上是:

1.override the adapter's getView method and setBackground for selected position

1。为所选位置重写适配器的getView方法和setBackground

2.setBackground of the view onItemClick and clear it for anther selection

2。设置onItemClick视图的背景,并为其他选择清除它

But none of them worked for me due to a weird behaviour: As I click on one item and highlight it, the fifth item after it is highlighted as well, and so on so forth as I scroll down the list.

但它们对我都不起作用,因为我有一种奇怪的行为:当我点击一个条目并突出显示它时,在它之后的第五个条目也被突出显示出来,等等。

Any suggestions? THX!

有什么建议吗?谢谢!

6 个解决方案

#1


22  

Use listView.setChoiceMode(int choiceMode);

使用列表视图。setChoiceMode(int choiceMode);

Parameters

参数

choiceMode One of CHOICE_MODE_NONE, CHOICE_MODE_SINGLE, or CHOICE_MODE_MULTIPLE from class android.widget.AbsListView

choiceMode之一的CHOICE_MODE_NONE, CHOICE_MODE_SINGLE,或CHOICE_MODE_MULTIPLE类android.widget.AbsListView

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

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

You also need to add MultiChoiceModeListener, you can have CHOICE_MODE_SINGLE

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

(android.widget.AbsListView.MultiChoiceModeListener)

(android.widget.AbsListView.MultiChoiceModeListener)

Refer to the sample below

参考下面的示例

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

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

#2


16  

this is for custom listactivity or ListFragment

这是用于自定义列表活动或列表片段。

highlight selected item in 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);
}

#3


2  

////@drawable/list_selector

/ / / / @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>

////////////////////and on 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"/>

///////////////////////listview Item

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

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

#4


1  

The reason you are highlighting multiple items is probably because you are either: reusing the whole view, or setting the background of both of these views to the same Drawable instance. If you have the same drawable on the screen twice, all events that happen to the first will happen to all the others, because that logic is implemented in the instance of the Drawable itself.

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

To solve this, either: do not re-use Views for multiple rows, or do not re-use Drawables for multiple rows (create a new one each time)

要解决这个问题,要么是:不要重用多个行的视图,要么不要重用多个行的Drawables(每次都创建一个新的)

I know this sounds resource intensive and it is, but unless you have a better solution figured out this is the easiest fix.

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

#5


0  

Because the items in the listviews mimics the ones at the top, they also mimics the background of them. You have to set every one of the background of your items in getView() function. In every item in getView() you have to set the background for both selected ones and the unselected ones.

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

#6


0  

use this as list selector:

使用这个作为列表选择器:

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

and this list_bg:

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

choose the preferred highlight color

选择首选的高光颜色

#1


22  

Use listView.setChoiceMode(int choiceMode);

使用列表视图。setChoiceMode(int choiceMode);

Parameters

参数

choiceMode One of CHOICE_MODE_NONE, CHOICE_MODE_SINGLE, or CHOICE_MODE_MULTIPLE from class android.widget.AbsListView

choiceMode之一的CHOICE_MODE_NONE, CHOICE_MODE_SINGLE,或CHOICE_MODE_MULTIPLE类android.widget.AbsListView

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

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

You also need to add MultiChoiceModeListener, you can have CHOICE_MODE_SINGLE

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

(android.widget.AbsListView.MultiChoiceModeListener)

(android.widget.AbsListView.MultiChoiceModeListener)

Refer to the sample below

参考下面的示例

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

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

#2


16  

this is for custom listactivity or ListFragment

这是用于自定义列表活动或列表片段。

highlight selected item in 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);
}

#3


2  

////@drawable/list_selector

/ / / / @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>

////////////////////and on 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"/>

///////////////////////listview Item

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

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

#4


1  

The reason you are highlighting multiple items is probably because you are either: reusing the whole view, or setting the background of both of these views to the same Drawable instance. If you have the same drawable on the screen twice, all events that happen to the first will happen to all the others, because that logic is implemented in the instance of the Drawable itself.

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

To solve this, either: do not re-use Views for multiple rows, or do not re-use Drawables for multiple rows (create a new one each time)

要解决这个问题,要么是:不要重用多个行的视图,要么不要重用多个行的Drawables(每次都创建一个新的)

I know this sounds resource intensive and it is, but unless you have a better solution figured out this is the easiest fix.

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

#5


0  

Because the items in the listviews mimics the ones at the top, they also mimics the background of them. You have to set every one of the background of your items in getView() function. In every item in getView() you have to set the background for both selected ones and the unselected ones.

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

#6


0  

use this as list selector:

使用这个作为列表选择器:

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

and this list_bg:

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

choose the preferred highlight color

选择首选的高光颜色