如何在ListView中突出显示按下的项目? [英] How to highlight pressed items in a ListView?

查看:74
本文介绍了如何在ListView中突出显示按下的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面使用此代码,但无法正常工作:如果我选择一个项目,则背景会更改.但是,如果我只关注项目而不选择项目,则背景也会发生变化.为什么?

I am using this code below, but it doesn't work properly: If I select an item, the background is changed. But the background change also if I put only focus on the item without selecting it. Why ?

已添加到我的列表视图:

Added to my listview:

android:listSelector="@drawable/bg_key"

@ drawable/bg_key

<?xml version="1.0" encoding="utf-8"?>
<selector
    android:id="@+id/myselector"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_activated="false"
        android:drawable="@color/activated_color" />

</selector>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="activated_color">#1d1d1d</color>
</resources>

推荐答案

您可以在选择器文件中使用 pressed 状态

You can use pressed state in your selector file

/drawable/list_selector.xml

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

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

</selector>

然后在您的listView中设置以下属性

then set following attribute in your listView

android:listSelector="@drawable/list_selector"

这篇关于如何在ListView中突出显示按下的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆