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

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

问题描述

已经阅读过一些相关的问题在这里的SO,通过Android文档和源代码,以及看着揣摩了这一点,但我很为难,但鉴于listSelector似乎只适用方式对选定的项目,我不会感到震惊......

我在main.xml中这里定义一个ListView:

 < XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    <的ListView
    机器人:ID =@机器人:ID /列表
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:fadingEdgeLength =5DP
    机器人:分隔=#000000
        机器人:dividerHeight =1DP
        机器人:listSelector =@可绘制/ list_selector/>
    <的TextView
    机器人:ID =@机器人:ID /空
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:重力=中心
        机器人:单线=假
        机器人:文本=@字符串/ message_list_empty/>
< /的FrameLayout>
 

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

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <! - 窗口失去焦点,禁用项目 - >
    <项目
        机器人:state_window_focused =假
        机器人:可绘制=@可绘制/ shape_row_disabled/>

    <! - 列表项将被禁用 - >
    <项目
        机器人:state_enabled =假
        机器人:state_focused =真
        机器人:STATE_ pressed =真
        机器人:可绘制=@可绘制/ shape_row_disabled/>
    <项目
        机器人:state_enabled =假
        机器人:state_focused =真
        机器人:可绘制=@可绘制/ shape_row_disabled/>
    <项目
        机器人:state_enabled =假
        机器人:可绘制=@可绘制/ shape_row_disabled/>

    <! - 列表项已启用,成为pressed  - >
    <项目
        机器人:state_focused =真
        机器人:STATE_ pressed =真
        机器人:可绘制=@可绘制/ shape_row_transition/>
    <项目
        机器人:state_focused =假
        机器人:STATE_ pressed =真
        机器人:可绘制=@可绘制/ shape_row_transition/>

    <! - 列表项有Enabled和所用 - >
    <项目
        机器人:state_focused =真
        机器人:可绘制=@可绘制/ shape_row_selected/>

    <! - 默认项 - >
    <项目
        机器人:可绘制=@可绘制/ shape_row/>
< /选择器>
 

引用的可绘制的形状,圆角矩形,像这样:

 < XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:形状=矩形>
    <梯度
        机器人:startColor =#EF6100
        机器人:centerColor =#FF8E00
        机器人:endColor =#EF6100
        机器人:角=270/>
    <角落
        机器人:bottomRightRadius =7DP
        机器人:bottomLeftRadius =7DP
        机器人:topLeftRadius =7DP
        机器人:topRightRadius =7DP/>
< /形状>
 

所以,最终的结果应该是有根据状态一@绘制/ shape_row背景项目在默认情况下,然后不同颜色的背景一个ListView。当我火了我的名单,一切正常的时候状态是积极的方面,比如一个项目获得焦点,是pressed等,但项目本身有一个透明的背景当没有被选中(即默认视图州)。我曾希望国家名单的最后一个规则,

 <项目机器人:可绘制=@可绘制/ shape_row/>
 

将捕获的状态,但由于某种原因它不工作。我已经感动周围的事物和尝试不同的设置,并没有什么。如果我修改,我用它来定义在ListView列表项,并尝试以特定的Andr​​oid添加row.xml文件:后台指向@可绘制/ shape_row,每一行获得正确的背景,但在pressed,上聚焦等状态无法处理(或至少,不能被看作是背景从不改变)。

任何人都可以点我朝着正确的方向吗?我是如此接近把这个一到床上,而是试图几乎一切后,就不能得到ListView的项目采取了默认的背景,通过Android的响应实施国家名单:listSelector

谢谢

编辑:

也只是尝试添加机器人:itemBackground =@可绘制/ shape_row到ListView main.xml中,而不出所料没有运气(该文档,它应该被用来指定菜单项的背景,但认为这是值得一拍摄列表项)。

另一件事尝试,加入机器人:可见=true将每一个选择和项目的定义。所述StateListDrawable文档似乎表明,在<一href="http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html#attr_android%3avisible">"Provides提拉的初始可见性状态;默认值为false,但加入这个什么也没有改变。

EDIT2:因此,基于Qberticus下面做了研究,似乎只能有一组按次列表选择的,这印证了列表选择行为。我还可以确认设置了android:drawSelectorOnTop不移动选择所选项目的前面,但是这当然是掩盖了项目本身,这样我只看到选择器本身(这在我的情况是有颜色的形状)

下面是列表看起来像背景设置:

由于集背景,存在外观没有变化时选择这些项。如果我改变了android:drawSelectorOnTop指令为true,并选择一个项目,我得到:

最后,如果我没有设定背景图片列表项,选择正常工作,但当然,没有背景图片为StateSelector的最后一个规则似乎并没有遵守(应该不是充当包罗万象)对非选择的项目意味着没有好的自定义圆角的形状:?

所以,我的问题仍然存在;如果我没有设置背景为每个列表项,可以看我的选择工作,但该项目没有背景的时候不要选择。如果我设置背景的项目,他们看起来伟大的,当没有选择,但背景模糊的选择。难道根本不可能有一个形状独特的列表项与工作选择?

感谢谁比谁能够在重量。

解决方案

想通了这一点,问题是,当我通过正确的设置listSelector为ListView的国家名单的android:listSelector = @可绘制/ list_selector,我还需要通过 Android的设置背景列表项到另一个状态列表:后台=@可绘制/ list_background

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

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:可见=真&GT;

    &LT;! - 列表项将被禁用 - &GT;
&LT;项目
        机器人:state_window_focused =假
        机器人:可绘制=@可绘制/ shape_row_disabled/&GT;

    &LT;! - 列表项的启用,在焦点,但没有被感动pssed(空闲)/ $ P $  - &GT;
    &LT;项目
        机器人:state_window_focused =真
        机器人:STATE_ pressed =假
        机器人:state_focused =假
        机器人:state_selected =假
        机器人:可绘制=@可绘制/ shape_row/&GT;

    &LT;! - 杂物箱 - &GT;
    &LT;项目
        机器人:可绘制=@机器人:彩色/透明/&GT;
&LT; /选择器&GT;
 

找到了答案在这里:

<一个href="http://stackoverflow.com/questions/2217753/changing-background-color-of-listview-items-on-android/2218270#2218270">Changing Android上的ListView项目的背景颜色

在我的列表中选择上面的结合,它完美的作品。

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天全站免登陆