相关疑难解决方法(0)

通过自定义选择器ListView项目背景

是否可以通过列表选择器将自定义背景应用于每个Listview项?

默认选择器指定@android:color/transparentstate_focused="false"大小写,但将其更改为某个自定义drawable不会影响未选中的项目.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>
Run Code Online (Sandbox Code Playgroud)

这就是我设置选择器的方式:

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:listSelector="@drawable/list_selector_background" />    
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助!

android listview

97
推荐指数
5
解决办法
15万
查看次数

标签 统计

android ×1

listview ×1