Android ListView状态列表未显示默认项目背景

Pau*_*ega 13 android listview state background

已经在SO上阅读了很多相关问题,并通过Android文档和源代码查看了这一点,但是我很难过,尽管listSelector似乎只对所选项目应用样式,但是我没有震惊......

我在main.xml中定义了Listview:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView  
    android:id="@android:id/list"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:fadingEdgeLength="5dp"
    android:divider="#000000"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />
    <TextView 
    android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:singleLine="false"
        android:text="@string/message_list_empty" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

引用的listSelector在这里(主要借用SDK中的默认Android状态列表:/android/platforms/android-8/data/res/drawable/list_selector_background.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- the window has lost focus, disable the items -->
    <item 
        android:state_window_focused="false"
        android:drawable="@drawable/shape_row_disabled" />

    <!-- the list items are disabled -->
    <item 
        android:state_enabled="false"
        android:state_focused="true" 
        android:state_pressed="true"
        android:drawable="@drawable/shape_row_disabled" />
    <item 
        android:state_enabled="false"
        android:state_focused="true" 
        android:drawable="@drawable/shape_row_disabled" />
    <item 
        android:state_enabled="false"
        android:drawable="@drawable/shape_row_disabled" />

    <!-- the list items are enabled and being pressed -->
    <item 
        android:state_focused="true" 
        android:state_pressed="true"
        android:drawable="@drawable/shape_row_transition" />
    <item 
        android:state_focused="false" 
        android:state_pressed="true"
        android:drawable="@drawable/shape_row_transition" />

    <!-- the list items are enabled and being used -->
    <item
        android:state_focused="true"
        android:drawable="@drawable/shape_row_selected" />

    <!-- the default item -->
    <item 
        android:drawable="@drawable/shape_row" />
</selector>
Run Code Online (Sandbox Code Playgroud)

引用的drawables是形状,圆角矩形,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle">
    <gradient 
        android:startColor="#EF6100"
        android:centerColor="#FF8E00" 
        android:endColor="#EF6100" 
        android:angle="270" /> 
    <corners 
        android:bottomRightRadius="7dp" 
        android:bottomLeftRadius="7dp"
        android:topLeftRadius="7dp" 
        android:topRightRadius="7dp" /> 
</shape>
Run Code Online (Sandbox Code Playgroud)

因此,最终结果应该是ListView,默认情况下具有@ drawable/shape_row背景的项目,然后根据状态具有不同的彩色背景.当我启动列表时,一切都可以正常运行状态,例如项目获得焦点,按下等等,但是当没有选择项目时,项目本身具有透明背景(即默认视图状态).我曾希望国家清单的最后一条规则,

<item android:drawable="@drawable/shape_row" />
Run Code Online (Sandbox Code Playgroud)

将捕获该状态,但由于某种原因它不起作用.我已经移动了一些东西,尝试了不同的设置,什么都没有.如果我编辑row.xml文件,我用它来定义ListView中的列表项,并尝试添加一个特定的android:background指向@ drawable/shape_row,每行得到正确的背景,但是按下,焦点,等状态无法处理(或至少,不能被视为背景永远不会改变).

任何人都能指出我在正确的方向吗?我很接近把这个放到床上,但在尝试了几乎所有东西后,只是无法获取ListView项目采取默认背景并通过android:listSelector响应实现的状态列表.

谢谢,

保罗

编辑:

还尝试将android:itemBackground ="@ drawable/shape_row"添加到main.xml中的ListView,毫不奇怪的是没有运气(文档声明它应该用于指定菜单项的背景,但认为值得用列表拍摄项目).

尝试了另一件事,为每个选择器和项目定义添加了android:visible ="true".StateListDrawable文档似乎表明"提供drawable的初始可见性状态;默认值为false",但添加此内容没有任何改变.

编辑2:所以,基于Qberticus下面的研究,似乎每个视图只能有一组列表选择器,这确认了列表选择器的行为.我还可以确认设置android:drawSelectorOnTop确实移动所选项目的选择器IN FRONT,但这当然模糊了项目本身,这样我只能看到选择器本身(在我的情况下是彩色形状).

以下是背景设置列表的样子:

带有背景设置的正常,未选择列表

由于设置了背景,选择项目时外观没有变化.如果我将android:drawSelectorOnTop指令更改为true并选择一个项目,我会得到:

选中,将drawSelectorOnTop设置为true

最后,如果我没有为列表项设置背景图像,选择器按预期工作,但当然,没有背景图像,因为StateSelector的最后一条规则似乎没有被遵循(不应该作为一个catchall?)对于未选择的项目,意味着没有漂亮的自定义圆形形状:

选中,没有背景

所以,我的问题仍然存在; 如果我没有为每个列表项设置背景,我可以看到选择器正在工作但是如果没有选择则项目没有背景.如果我为项目设置背景,则在未选中时它们看起来很棒,但背景会遮挡选择器.是否根本无法使用具有工作选择器的独特形状的列表项?

感谢任何能够减肥的人.

Pau*_*ega 9

想出这个问题,问题是当我正确地将ListView的listSelector设置为状态列表时android:listSelector="@drawable/list_selector",我还需要通过列表项的背景设置为另一个状态列表android:background="@drawable/list_background"

在list_background.xml状态列表中,我有:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:visible="true">

    <!-- the list items are disabled -->
<item 
        android:state_window_focused="false"
        android:drawable="@drawable/shape_row_disabled" />

    <!-- the list items are enabled, in focus but not being touched/pressed (idle) -->
    <item 
        android:state_window_focused="true"
        android:state_pressed="false"
        android:state_focused="false"
        android:state_selected="false"
        android:drawable="@drawable/shape_row" />

    <!-- the catch-all -->
    <item 
        android:drawable="@android:color/transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)

在这里找到答案:

在Android上更改ListView项目的背景颜色

结合我上面的列表选择器,它完美地工作.