Android-ListView项目未更改其状态

Android-ListView项目未更改其状态,android,listview,background,selector,drawable,Android,Listview,Background,Selector,Drawable,我目前在ListView项目上面临后台绘图的问题。 我有一个ListView XML、一个Item XML和一个用于一个Item可以具有的不同状态的可绘制XML 问题是,当我单击或按下其中一项时,视觉上没有任何变化,但单击是有效的,因为我重写的onItemClick()方法被调用,其代码被执行。。。好像我没有设置@background参数 layout/my_activity.xml(包含listview): 布局/列表视图\项目\ a.xml: <?xml version="1.0"

我目前在ListView项目上面临后台绘图的问题。 我有一个ListView XML、一个Item XML和一个用于一个Item可以具有的不同状态的可绘制XML

问题是,当我单击或按下其中一项时,视觉上没有任何变化,但单击是有效的,因为我重写的onItemClick()方法被调用,其代码被执行。。。好像我没有设置@background参数

layout/my_activity.xml(包含listview):


布局/列表视图\项目\ a.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="wrap_content"
    android:background="@drawable/listview_item_a_d"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        ... />

    <TextView
        ... />

    <TextView
        ... />

</LinearLayout>

可绘制/列表视图\u项目\u a\u d.xml:

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

    <item android:state_selected="true"><shape>
            <gradient android:angle="270" android:endColor="#bbbbbb" android:startColor="#e9e9e3" />
        </shape></item>
    <item android:state_enabled="true"><shape>
            <gradient android:angle="270" android:endColor="#ecca2e" android:startColor="#f9f7c9" />
        </shape></item>
    <item><shape>
            <solid android:color="@color/gray_dark" />
        </shape></item>

</selector>

首先,触摸模式中没有持续的选定或聚焦状态

您可以通过使用
state\u activated
绕过此问题

您可以通过将列表的选择模式设置为single或multiple(默认为none)来实现这一点

然后在选择器XML中使用激活状态:

<item android:state_activated="true">
    <shape>   
        <gradient android:angle="270" android:endColor="#bbbbbb" droid:startColor="#e9e9e3" />   
    </shape>
</item> 


请注意,
state\u activated
用于API 11+。。。对于以前的版本,我相信您必须使用自定义适配器中的数组来跟踪所选状态,并使用该数组在适配器
getView
方法中设置背景色/可绘制。

我的ListView中有一个适配器,问题是我在getView方法中更改了视图的背景,因此我认为选择器被拉到后面,或者根本没有拉。不管怎样,我刚刚删除了那些代码行,现在它可以正常工作了,谢谢

谢谢,但这不是问题所在。我的ListView有一个适配器,问题是我在getView方法中更改了视图的backgound,所以我认为选择器被拖后了,或者根本没有被画出来。不管怎样,我刚刚删除了那些代码行,现在它可以正常工作了,谢谢!
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
<item android:state_activated="true">
    <shape>   
        <gradient android:angle="270" android:endColor="#bbbbbb" droid:startColor="#e9e9e3" />   
    </shape>
</item>