是否可以通过列表选择器为每个ListView项应用自定义背景?
默认选择器在state_focused =“false”
情况下指定@android:color/transparent
,但将其更改为某些自定义绘图不会影响未选中的项目。Romain Guy似乎在这个回答中建议这是可能的。
我目前通过在每个视图上使用自定义背景并在选择/聚焦/其他时隐藏它以显示选择器来实现相同的效果,但将所有内容定义在一个地方将更加优雅。
供参考,这是我用于尝试使其工作的选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:drawable="@drawable/list_item_gradient" />
<!-- 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_background_disabled" />
<item android:state_focused="true" android:state_enabled="false"
android:drawable="@drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/list_selector_background_transition" />
<item android:state_focused="true"
android:drawable="@drawable/list_selector_background_focus" />
</selector>
这是我设置选择器的方式:
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="@drawable/list_selector_background" />