ListView项后台通过自定义选择 [英] ListView item background via custom selector

查看:201
本文介绍了ListView项后台通过自定义选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过列表选择应用自定义背景,每个ListView项?<​​/ P>

默认选择指定 @android:彩色/透明 state_focused =假的情况下,但改变这对一些自定义绘制不影响未选择的项目。罗曼盖伊似乎暗示<一个href="http://stackoverflow.com/questions/2217753/changing-background-color-of-listview-items-on-android/2218270#2218270">in该答案的,这是可能的。

目前,我正在实现通过使用自定义背景上的每个视图,并将它们隐藏在项目被选中/聚焦/不管所以选择显示效果相同,但它会是更优雅让这一切定义一个地方。

作为参考,这是我使用,试图得到这个工作的选择:

 &LT;选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;

    &LT;项目安卓state_focused =假
        机器人:可绘制=@可绘制/ list_item_gradient/&GT;

    &LT;! - 尽管这两个指向同一个资源,有两种状态,以便在将绘制出来的pressed状态时无效本身。 - &GT;
    &LT;项目安卓state_focused =真正的机器人:state_enabled =假
        机器人:STATE_ pressed =真
        机器人:可绘制=@可绘制/ list_selector_background_disabled/&GT;
    &LT;项目安卓state_focused =真正的机器人:state_enabled =假
        机器人:可绘制=@可绘制/ list_selector_background_disabled/&GT;

    &LT;项目安卓state_focused =真正的机器人:STATE_ pressed =真
        机器人:可绘制=@可绘制/ list_selector_background_transition/&GT;
    &LT;项目安卓state_focused =假的Andr​​oid版本:STATE_ pressed =真
        机器人:可绘制=@可绘制/ list_selector_background_transition/&GT;

    &LT;项目安卓state_focused =真
        机器人:可绘制=@可绘制/ list_selector_background_focus/&GT;

&LT; /选择器&GT;
 

这也是为什么我设置的选择:

 &LT;的ListView
    机器人:ID =@机器人:ID /列表
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:listSelector =@可绘制/ list_selector_background/&GT;
 

在此先感谢您的帮助!

解决方案

我已经受挫这个自己终于解决了这个问题。由于罗曼盖伊暗示到,还有另一种状态,机器人:state_selected,你必须使用。使用状态绘制你的列表项的背景,并使用不同的状态绘制你的清单listSelector:

list_row_layout.xml:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =机器人:ATTR /列表preferredItemHeight
    机器人:背景=@可绘制/ listitem_background
    &GT;
...
&LT; / LinearLayout中&GT;
 

listitem_background.xml:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;
    &LT;项目安卓state_selected =真正的机器人:可绘制=@颜色/安卓:透明/&GT;
    &LT;项目机器人:可绘制=@可绘制/ listitem_normal/&GT;
&LT; /选择器&GT;
 

layout.xml包含的ListView:

  ...
&LT;的ListView
   机器人:layout_width =FILL_PARENT
   机器人:layout_height =WRAP_CONTENT
   机器人:listSelector =@可绘制/ listitem_selector
   /&GT;
...
 

listitem_selector.xml:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;
    &LT;项目的android:STATE_ pressed =真正的机器人:可绘制=@可绘制/ listitem_ pressed/&GT;
    &LT;项目安卓state_focused =真正的机器人:可绘制=@可绘制/ listitem_selected/&GT;
&LT; /选择器&GT;
 

Is it possible to apply a custom background to each Listview item via the list selector?

The default selector specifies @android:color/transparent for the state_focused="false" case, but changing this to some custom drawable doesn't affect items that aren't selected. Romain Guy seems to suggest in this answer that this is possible.

I'm currently achieving the same affect by using a custom background on each view and hiding it when the item is selected/focused/whatever so the selector is shown, but it'd be more elegant to have this all defined in one place.

For reference, this is the selector I'm using to try and get this working:

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

And this is how I'm setting the selector:

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:listSelector="@drawable/list_selector_background" />    

Thanks in advance for any help!

解决方案

I've been frustrated by this myself and finally solved it. As Romain Guy hinted to, there's another state, "android:state_selected", that you must use. Use a state drawable for the background of your list item, and use a different state drawable for listSelector of your list:

list_row_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="@drawable/listitem_background"
    >
...
</LinearLayout>

listitem_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@color/android:transparent" />
    <item android:drawable="@drawable/listitem_normal" />
</selector>

layout.xml that includes the ListView:

...
<ListView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:listSelector="@drawable/listitem_selector"
   />
...

listitem_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/listitem_pressed" />
    <item android:state_focused="true" android:drawable="@drawable/listitem_selected" />
</selector>

这篇关于ListView项后台通过自定义选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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