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

标签 android listview

我正在尝试将选择器应用于我的 ListView ,这是一个自定义 ListView ,我尝试了很多选项但没有任何效果。任何帮助将不胜感激。我附上我的代码。提前致谢

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>

黄色背景效果有效但蓝色效果没有发生,请帮助 提前致谢

最佳答案

将此选择器设置为每一行的背景而不是 ListView 。

关于Android:ListView 选择器无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20605413/

相关文章:

android - 图片显示在 Android Studio 中但不显示在手机上

android - 如何在我的 android gridview 中显示来自互联网的图像并显示完整图像?

Android:如何在 CardView 中插入 RecyclerView?

android - 从应用程序 gradle 运行库构建作为 build.dependsOn libraryBuild 任务,而不包括作为应用程序 gradle 文件中的依赖项

android - 带有 colSpan 和 rowSpan 的 GridView [Android]

android - ListView:drawSelectorOnTop 在主题中被忽略?

android - android ListView 中的异常

java - 在 Recycler View 的情况下,等效的 listview.setSelection 是什么

android - CoroutineScope 取消

android - 在ListView中添加图像