我希望能够通过代码禁用用户选择行时出现的高亮显示(listSelector),但我不想禁用onClick和enabled设置(仍然要监听点击事件,只是想去掉高亮)。
我希望能够通过代码禁用用户选择行时出现的高亮显示(listSelector),但我不想禁用onClick和enabled设置(仍然要监听点击事件,只是想去掉高亮)。
ListView
XML中,指定android:listSelector="@android:color/transparent"
。只需创建一个具有透明颜色的可绘制对象,例如:
<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>
然后可以通过代码或XML进行设置:
listView.setSelector(R.drawable.my_transparent_selector);
android:cacheColorHint="@android:color/transparent"
android:listSelector="@android:color/transparent"
您的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>
完成
listview.setSelector(new ColorDrawable(Color.TRANSPARENT));