java Android - 禁用 ListView 选择突出显示但保持 OnClick 启用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17033350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:46:15  来源:igfitidea点击:

Android - Disable ListView Selection Highlight but Keep OnClick enabled

javaandroidlistview

提问by Abdalrahman Shatou

I want to disable the highlight that appears when the user selects a row (listSelector) from code. I don't want to disable the onClick and enabled settings (I still want to listen to clicks, just want to remove the highlight).

我想禁用当用户从代码中选择一行 (listSelector) 时出现的突出显示。我不想禁用 onClick 和启用设置(我仍然想听点击,只是想删除突出显示)。

回答by Dororo

Specify android:listSelector="@android:color/transparent"in your ListViewXML.

android:listSelector="@android:color/transparent"在您的ListViewXML 中指定。

回答by Nicolas Jafelle

Just create a drawable that has a transparent color in it, something like this:

只需创建一个具有透明颜色的可绘制对象,如下所示:

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

<item android:state_window_focused="false" android:drawable="@android:color/transparent"/>

<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true"  android:state_enabled="false" android:drawable="@drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true"  android:state_pressed="true" android:drawable="@color/transparent" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@color/transparent" />
<item android:state_focused="true"  android:drawable="@drawable/list_focused_holo" />

</selector>

And then set by code or by XML:

然后通过代码或 XML 设置:

listView.setSelector(R.drawable.my_transparent_selector);

The javadoc for this method says:

此方法的 javadoc 说:

Set a Drawable that should be used to highlight the currently selected item.

设置一个 Drawable,用于突出显示当前选定的项目。

and the XML attribute is:

并且 XML 属性是:

android:listSelector

机器人:列表选择器

You can play with all the states, remember that you also have the focus state.

你可以玩所有的状态,记住你也有焦点状态。

回答by Hiren Patel

I have done this way:

我已经这样做了:

By adding two properties of ListView.

通过添加ListView 的两个属性。

android:cacheColorHint="@android:color/transparent"
android:listSelector="@android:color/transparent"

Your ListViewshould looks like below:

您的 ListView应如下所示:

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:cacheColorHint="@android:color/transparent"
    android:listSelector="@android:color/transparent">

</ListView>

Done

完毕

回答by joecizac

try listview.setSelector(new ColorDrawable(Color.TRANSPARENT));

尝试 listview.setSelector(new ColorDrawable(Color.TRANSPARENT));

回答by joecizac

The highlight effect is a style on the listSelector. You can override the listSelector style.

高亮效果是 listSelector 上的一种样式。您可以覆盖 listSelector 样式。

This is a example with a listview : Android: disabling highlight on listView click

这是一个带有列表视图的示例:Android: 在 listView 单击上禁用突出显示