Android:ListView选择器无法正常工作

3

我正在尝试将选择器应用到我的列表视图中,它是一个自定义列表视图,我尝试了很多选项但都不起作用。非常感谢任何帮助。我附上了我的代码。提前致谢。

selector_listview.xml

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

</selector>

layout_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff" >

    <ListView
        android:id="@+id/lvChatList"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/rlHeader"
        android:background="@drawable/selector_expandable_listview"
        android:listSelector="@drawable/selector_expandable_listview" >

    </ListView>

    <TextView
        android:id="@+id/tvNoChats"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="No Chats"
        android:visibility="gone" />

</RelativeLayout>

黄色背景效果有效,但蓝色效果不起作用,请帮忙解决。 提前感谢。

2个回答

20
将此选择器设置为每行的背景,而不是列表视图。

我有另一个可扩展的列表视图,其子项始终处于展开状态。我想做的就是在子项被点击时添加选择器,就像我们在普通列表视图中所做的那样。这是否可能? - Rohan
同样的方法也可以奏效。只是不要将此选择器设置为标题布局的背景。 - vipul mittal
你的意思是我应该只为子布局设置选择器吗? - Rohan

0

您的ListView将使用自定义布局作为行项目。因此,请尝试将该布局的背景设置为android:background="@drawable/selector_listview"。还要确保在您的selector_listview.xml中有默认项<item android:drawable="@drawable/your_default_background_for_row_item" />


是的...我也按照Vipul先生建议的做了,它起作用了,谢谢Prashant。 - Rohan
我有一个与可扩展列表视图相关的问题,其子项始终处于展开状态。我想做的就是在单击其子项时向其添加选择器,就像我们在普通列表视图中所做的那样。这是否可能? - Rohan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接