【问题标题】:ListView listSelector not hidingListView listSelector 不隐藏
【发布时间】:2024-11-30 17:45:01
【问题描述】:

我有一个带有 listSelector 可绘制的 ListView 来指示所选项目。

<ListView
    android:id="@+id/roadSignListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:listSelector="@drawable/listview" >
</ListView>

drawable/listview 是一个选择器:

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

light_gray 在哪里

<?xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:centerColor="#cccccc"
        android:endColor="#cccccc"
        android:startColor="#cccccc" />    
</shape>

这是在答案in this question的帮助下完成的。

我的问题与另一个问题相同:背景悬在后面。我目前使用的提供的解决方案不起作用。直接使用颜色也是同样的问题

<ListView
    android:id="@+id/roadSignListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:listSelector="#cccccc" >
</ListView>

我已将所需的最低 API 级别设置为 14,我的目标是最新的 19。我正在运行 Android 4.4.2 的新 Nexus 7 上进行测试。

接下来我应该尝试什么?

【问题讨论】:

    标签: android listview android-listview android-drawable


    【解决方案1】:

    请为您的可绘制对象定义状态,例如 state_pressed 等。

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    
        <item 
            android:state_pressed="true" 
             android:drawable="#333333" />
    
        <item 
            android:state_pressed="false"
          android:drawable="#334455"/>
       <item 
          android:drawable="#334455"/>
    
    
    
    </selector>
    

    【讨论】:

    • 为什么?如果之前没有找到其他匹配项,则默认使用最后一项。因此在这种情况下,唯一没有状态的项目应该完全正常工作。
    • @njzk2 你不知道我在 listSelector 上挣扎了多少时间(一周+),解决方案非常简单,我不得不将没有状态的项目从第一个移到成为最后一个。我不知道它对其他人是如何工作的,因为在网上几乎所有的例子中它都是第一个
    • @Gavriel:很奇怪。这是一个 statelistdrawable (developer.android.com/guide/topics/resources/…) 和文档状态 the state list is traversed top to bottom and the first item that matches the current state is used
    • @njzk2,这很有意义(现在,我知道 :),请在此处查看我的答案:*.com/questions/27214144/…,这不完全是因为您在这里所说的,但这让我明白改变顺序可能会解决它,它确实