突出显示android中listview中的选定项目

时间:2023-01-19 10:40:56

I am having 1 list view contactslist. I wrote the code for highlighting the selected item in the ListView. It is working. When I click on 1 item it is highlighting that item but the problem is if I click on other item it is highlighting that too. I want to highlight the selected item only. The previous selection will have to gone when I click on another item.

我有1个列表视图联系人列表。我编写了用于突出显示ListView中所选项的代码。这是工作。当我点击1项时,它突出显示该项目,但问题是如果我点击其他项目也突出显示该项目。我想只突出显示所选项目。当我点击另一个项目时,之前的选择将不复存在。

arg1.setBackgroundResource(R.drawable.highlighter);

This is the code in the click listener using to highlight the selected item. plz help me.

这是单击侦听器中用于突出显示所选项的代码。请帮助我。

Update
I'm setting the background of the rows in the adapter:

更新我正在设置适配器中行的背景:

public int[] colors = new int[]{0xFFedf5ff, 0xFFFFFFFF}; 
public int colorPos; 

[...]
colorPos = position % colors.length; 
row.setBackgroundColor(colors[colorPos]);

4 个解决方案

#1


107  

ListViews by default don't have a choiceMode set (it's set to none), so the current selection is not indicated visually.

默认情况下,ListView没有设置choiceMode(它设置为none),因此当前选择不会以可视方式显示。

To change this, you just need to set the choiceMode attribute of your ListView to singleChoice.
If you'd like custom background for the selected items in your list, you should also set the listSelector attribute. There you can specify not only colors, but drawables (images, layer-/state-drawables).

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

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

If you don't use a ListView directly, but a ListActivity, then these attributes need to be set from code, so you should extend your activity's onCreate method with these lines:

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

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

So if you were using a click listener to change the background of the selected row, remove that from your code, and use the proper method from above.

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

Reply to the update

回复更新

If you set the background from your getView method, instead of using a static color, apply a state list drawable to the row background with duplicateParentState set to true. This way it will change its display based on the current state of the item: normal, focused, pressed, etc.

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

#2


20  

In the listview xml add the "singleChoice" mode

在listview xml中添加“singleChoice”模式

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    (...) >
</ListView>

In the list item layout add

在列表项布局中添加

android:background="?android:attr/activatedBackgroundIndicator

机器人:背景=“机器人:ATTR / activatedBackgroundIndicator

example

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

     <!-- your item content-->

</LinearLayout> 

#3


7  

A better way is in your theme, @drawable/list_selector

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

list_selector.xml :

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

then set background for root of your list_row.xml android:background="?android:attr/activatedBackgroundIndicator"

然后设置list_row.xml的root的背景android:background =“?android:attr / activatedBackgroundIndicator”

#4


0  

Try this at onListItemClick

在onListItemClick上试试这个

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

It highlights the selection.

它突出了选择。

#1


107  

ListViews by default don't have a choiceMode set (it's set to none), so the current selection is not indicated visually.

默认情况下,ListView没有设置choiceMode(它设置为none),因此当前选择不会以可视方式显示。

To change this, you just need to set the choiceMode attribute of your ListView to singleChoice.
If you'd like custom background for the selected items in your list, you should also set the listSelector attribute. There you can specify not only colors, but drawables (images, layer-/state-drawables).

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

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

If you don't use a ListView directly, but a ListActivity, then these attributes need to be set from code, so you should extend your activity's onCreate method with these lines:

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

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

So if you were using a click listener to change the background of the selected row, remove that from your code, and use the proper method from above.

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

Reply to the update

回复更新

If you set the background from your getView method, instead of using a static color, apply a state list drawable to the row background with duplicateParentState set to true. This way it will change its display based on the current state of the item: normal, focused, pressed, etc.

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

#2


20  

In the listview xml add the "singleChoice" mode

在listview xml中添加“singleChoice”模式

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    (...) >
</ListView>

In the list item layout add

在列表项布局中添加

android:background="?android:attr/activatedBackgroundIndicator

机器人:背景=“机器人:ATTR / activatedBackgroundIndicator

example

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

     <!-- your item content-->

</LinearLayout> 

#3


7  

A better way is in your theme, @drawable/list_selector

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

list_selector.xml :

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

then set background for root of your list_row.xml android:background="?android:attr/activatedBackgroundIndicator"

然后设置list_row.xml的root的背景android:background =“?android:attr / activatedBackgroundIndicator”

#4


0  

Try this at onListItemClick

在onListItemClick上试试这个

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

It highlights the selection.

它突出了选择。