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

Abd*_*tou 5 java android listview

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

Dor*_*oro 17

android:listSelector="@android:color/transparent"ListViewXML中指定.


Nic*_*lle 6

只需创建一个透明颜色的drawable,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

然后按代码或XML设置:

listView.setSelector(R.drawable.my_transparent_selector);
Run Code Online (Sandbox Code Playgroud)

这个方法的javadoc说:

设置应该用于突出显示当前所选项目的Drawable.

并且XML属性是:

机器人:listSelector

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