如何使 ListView 在触摸时不响应(更改背景)?

How to make ListView not respond (change background) on touch?

我有一个显示一些惰性项目的 ListView,因此当用户触摸列表视图或项目时,这不应更改它们的背景。事实证明这很难做到: 到目前为止我尝试了什么: Java:

listView.setLongClickable(false);
listView.setClickable(false);
listView.setFocusable(false);

这没有任何影响。如果我触摸列表视图,背景会发生变化。

在xml我试过了;

<ListView
    android:id="@android:id/list"
    android:choiceMode="none"
    android:clickable="true"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:listSelector="@null" />

将 listSelector 设置为 @null 是我在其他地方看到的有关堆栈溢出的内容。无效。

我也试过在代码中将每个项目的背景设置为透明。

convertView.setBackgroundResource(R.color.transparent);

没有。每次触摸时,背景总是会变为蓝色。 如何让我的列表视图在触摸时不改变项目背景?

为此,您需要为列表视图制作自定义背景,例如

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

将名称为 listbg.xml 的 xml 文件放入您的可绘制文件夹中

<ListView
    android:id="@+id/listt"
    android:choiceMode="none"
    android:clickable="true"
    android:background="@drawable/listbg"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:listSelector="@null" />

您可能正在为具有不同选定状态背景的列表项使用布局。

查看您对列表项使用的布局并将其更改为不包含该背景。如果您使用默认 (android.R.layout.simple_list_item) 布局,您可以创建和使用自己的布局。

您可以在 Adapter getView() 中设置 view.setEnabled(false); - 它会移除背景效果。

试试这个,对我有用

<ListView 
android:listSelector="@android:color/transparent" 
android:cacheColorHint="@android:color/transparent"
/>