【问题标题】:Android - ListView Item not changing its stateAndroid - ListView 项目不改变其状态
【发布时间】:2012-07-04 21:47:34
【问题描述】:

我目前正面临 ListView 项目上的背景可绘制对象的问题。 我有一个 ListView XML、一个 Item XML 和一个 Drawable XML,用于表示一个项目可以具有的不同状态。

问题是,当我单击或按下其中一项时,视觉上没有任何变化,但单击有效,因为调用了我覆盖的 onItemClick() 方法并执行了它的代码......就像我没有设置 @背景参数!

layout/my_activity.xml(包含列表视图):

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

    <include ... />

    <include ... />

    <View
        ... />

    <ListView
        android:id="@+id/listViewPacks"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="@color/blue_vdark"
        android:dividerHeight="2dp" >
    </ListView>

</LinearLayout>

布局/listview_item_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>

drawable/listview_item_a_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>

【问题讨论】:

    标签: android listview background selector drawable


    【解决方案1】:

    首先,在触摸模式下没有持久的选择或聚焦状态。

    您可以使用state_activated 解决此问题。

    您可以通过将列表的选择模式设置为单个或多个(默认为无)来实现此目的。

    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    

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

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

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

    【讨论】:

    • 谢谢,但这不是问题所在。我的 ListView 有一个适配器,问题是我在 getView 方法中更改了视图的背景,所以我认为选择器被绘制在后面,或者根本没有绘制。无论如何,我只是删除了这些代码行,现在它可以正常工作了,谢谢!
    【解决方案2】:

    我的 ListView 有一个适配器,问题是我在 getView 方法中更改了视图的背景,所以我认为选择器被绘制在后面,或者根本没有绘制。无论如何,我刚刚删除了这些代码行,现在它可以正常工作了,谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      相关资源
      最近更新 更多