Android ListView 状态列表不显示默认项目背景 [英] Android ListView State List not showing default item background

查看:19
本文介绍了Android ListView 状态列表不显示默认项目背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已在 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"><列表视图android:id="@android:id/list"android:layout_width="fill_parent"android:layout_height="fill_parent"安卓:fadingEdgeLength="5dp"安卓:分频器="#000000"安卓:dividerHeight="1dp"android:listSelector="@drawable/list_selector"/><文本视图android:id="@android:id/empty"android:layout_width="fill_parent"android:layout_height="fill_parent"机器人:重力=中心"机器人:单行=假"android:text="@string/message_list_empty"/></框架布局>

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

<?xml version="1.0" encoding="utf-8"?><选择器xmlns:android="http://schemas.android.com/apk/res/android"><!-- 窗口失去焦点,禁用项目--><项目机器人:state_window_focused="假"android:drawable="@drawable/shape_row_disabled"/><!-- 列表项被禁用--><项目安卓:状态启用=假"安卓:state_focused="真"安卓:state_pressed="真"android:drawable="@drawable/shape_row_disabled"/><项目安卓:状态启用=假"安卓:state_focused="真"android:drawable="@drawable/shape_row_disabled"/><项目安卓:状态启用=假"android:drawable="@drawable/shape_row_disabled"/><!-- 列表项已启用并被按下--><项目安卓:state_focused="真"安卓:state_pressed="真"android:drawable="@drawable/shape_row_transition"/><项目android:state_focused="假"安卓:state_pressed="真"android:drawable="@drawable/shape_row_transition"/><!-- 列表项已启用并正在使用--><项目安卓:state_focused="真"android:drawable="@drawable/shape_row_selected"/><!-- 默认项--><项目android:drawable="@drawable/shape_row"/></选择器>

引用的drawables是形状,圆角矩形,像这样:

<?xml version="1.0" encoding="utf-8"?><形状 xmlns:android="http://schemas.android.com/apk/res/android"android:shape="矩形"><梯度android:startColor="#EF6100"android:centerColor="#FF8E00"安卓:endColor="#EF6100"安卓:角度=270"/><角落安卓:bottomRightRadius="7dp"机器人:bottomLeftRadius="7dp"机器人:topLeftRadius="7dp"android:topRightRadius="7dp"/></形状>

因此,最终结果应该是一个 ListView,其中项目默认具有 @drawable/shape_row 背景,然后根据状态使用不同颜色的背景.当我启动我的列表时,在状态处于活动状态时一切正常,例如项目获得焦点、被按下等,但是当没有选择项目时(即默认视图状态),项目本身具有透明背景.我曾希望州列表的最后一条规则,

<item android:drawable="@drawable/shape_row"/>

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

任何人都可以在这里指出正确的方向吗?我已经快要把这个放在床上了,但是在尝试了几乎所有事情之后,就是无法让 ListView 项目采用默认背景并通过 android:listSelector 响应已实现的状态列表.

谢谢,

保罗

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

尝试了另一件事,将 android:visible="true" 添加到每个选择器和项目定义中.StateListDrawable 文档似乎表明

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

Have read over a number of related questions here at SO, as well as looked through the Android docs and source to try to figure this out, but I am stumped, although given that the listSelector seems to only apply styles on selected items, I'm not shocked...

I have a Listview defined in main.xml here:

<?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>

The listSelector referenced is here (borrowed largely from the default Android State List found in the SDK: /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>

The drawables referenced are shapes, rounded rectangles, like so:

<?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>

So, the end result should be a ListView with items with a @drawable/shape_row background by default, and then different colored backgrounds depending on the state. When I fire up my list, everything works fine in terms of when states are active, such as an item gets focus, is pressed, etc, but the items themselves have a transparent background when none are selected (i.e. the default view state). I had hoped that the last rule of the State List,

<item android:drawable="@drawable/shape_row" />

would capture that state, but for some reason it is not working. I have moved things around and tried different settings, and nothing. If I edit the row.xml file which I use to define the list items in the ListView and try to add a specific android:background that points to @drawable/shape_row, every row gets the correct background, BUT on pressed, on focus, etc states fail to process (or at least, can't be seen as the background never changes).

Can anyone point me in the right direction here? I am SO close to putting this one to bed, but after trying nearly everything, just can't get the ListView items to take a default background AND respond to the implemented State List via android:listSelector.

Thanks,

Paul

EDIT:

Also just tried adding android:itemBackground="@drawable/shape_row" to the ListView in main.xml, and unsurprisingly no luck (the docs state it should be used to specify menu items' backgrounds, but thought it was worth a shot with list items).

Another thing tried, adding android:visible="true" to each of the selector and item definitions. The StateListDrawable docs seem to indicate that the "Provides initial visibility state of the drawable; the default value is false", but adding this changed nothing.

EDIT2: So, based on the research done below by Qberticus, it seems that there can be only one set of list selectors per view, which confirms the list selector behavior. I can also confirm that setting the android:drawSelectorOnTop does move the selector IN FRONT of the selected item, but this of course obscures the item itself such that I only see the selector itself (which in my case is a colored shape).

Here is what the list looks like with backgrounds set:

Due to the set background, there is no change in appearance when items are selected. If I change the android:drawSelectorOnTop directive to true and select an item, I get:

And finally, if I set no background image for the list items, the selector works as expected, but of course, there are no background images as the last rule of the StateSelector does not appear to be followed (shouldn't it act as a catchall?) for non-selected items meaning no nice custom rounded shapes:

So, my problem remains; if I don't set a background for each list item, I can see the selector working BUT the items have no background when not selected. If I set a background for the item, they look great when not selected, but the background obscures the selector. Is it simply not possible to have a uniquely shaped list item with working selectors?

Thanks for anyone else who can weight in.

解决方案

Figured this out, the issue was that while I was setting the listSelector for the ListView to the State List correctly via android:listSelector="@drawable/list_selector", I also needed to set the background for the list item to another State List via android:background="@drawable/list_background"

In the list_background.xml state list, I have:

<?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>

Found the answer here:

Changing background color of ListView items on Android

In conjunction with my list selector above, it works perfectly.

这篇关于Android ListView 状态列表不显示默认项目背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆