在 Android 的列表视图中仅突出显示所选项目

发布于 2024-11-04 07:26:49 字数 579 浏览 4 评论 0原文

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

I have a list view contactslist. I wrote the code for highlighting the selected item in the ListView. It is working. When I click on one item it is highlighting that item, but the problem is if I click on another item it is highlighting that one too. I want to highlight the selected item only. The previous selection should go away when I click on another item.

arg1.setBackgroundResource(R.drawable.highlighter);

This is the code in the click listener using to highlight the selected item. How can I fix this?

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]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伴我心暖 2024-11-11 07:26:49

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

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

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

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

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

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

回复更新

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

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

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

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.

寂寞笑我太脆弱 2024-11-11 07:26:49

在listview xml中添加“singleChoice”模式

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:background="?android:attr/activatedBackgroundIndicator">

     <!-- your item content-->

</LinearLayout> 

In the listview xml add the "singleChoice" mode

<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

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> 
甜心小果奶 2024-11-11 07:26:49

更好的方法是
在你的主题中,
@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" 的根设置背景

A better way is
in your theme,
@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"/>

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

凹づ凸ル 2024-11-11 07:26:49

尝试一下 onListItemClick

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

它会突出显示所选内容。

Try this at onListItemClick

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

It highlights the selection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文