ListView项选中状态不工作状态、工作、ListView

2023-09-13 00:06:57 作者:°有些人,错过就是一辈子

所以我有一个ListView和我想要改变的每一个项目的背景和文本的颜色。这ListView控件是一个ListFragment内。我的code膨胀的 onCreateView 的布局和膨胀的每个项目中的 NewView的的布局。

安卓STATE_ pressed =真正的工作正常,每当我preSS在一个项目的背景更改该颜色。但是,在选择项目时,无论是背景色或文本颜色的变化,即使我已经定义了一个项目与机器人:state_selected =真正的中选择

编辑:我使用的SDK 11级(的Andr​​oid 3.0)和摩托罗拉Xoom

该列表片段布局:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的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/>
< / LinearLayout中>
 

列表项的布局:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直
    机器人:填充=25dp
    机器人:背景=@可绘制/ list_item_bg_selector>
    < TextView的机器人:ID =@ + ID / FORM_TITLE
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =@扪/ text_size_xlarge
        机器人:TEXTSTYLE =黑体
        机器人:文字颜色=@可绘制/ list_item_text_selector/>
    < TextView的机器人:ID =@ + ID / form_subtitle
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =@扪/ text_size_medium
        机器人:TEXTSTYLE =正常
        机器人:layout_marginTop =5DP
        机器人:文字颜色=@可绘制/ list_item_text_selector/>
< / LinearLayout中>
 

的背景选择:

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目
        机器人:STATE_ pressed =真
        机器人:可绘制=@色/白/>
    <项目
        机器人:state_selected =真
        机器人:可绘制=@可绘制/ list_item_bg_selected/>
    <项目
        机器人:可绘制=@色/ list_bg/>
< /选择器>
 
电子表格列项无法选中,点不动的那种

文本选择:

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目
        机器人:state_selected =真
        机器人:可绘制=@色/白/>
    <项目
        机器人:可绘制=@色/ list_text_blue/>
< /选择器>
 

解决方案

答案是使用的android:而不是选择state_activated =真正的状态, 州。更多内容点击这里:​​ ListFragment项目选择的背景

So I have a ListView and I want to change the color of each items background and text. This ListView is inside a ListFragment. My code inflates the layout in the onCreateView and inflates the layout of each item in the newView.

The android:state_pressed="true" is working fine, whenever I press in one item the background changes to that color. But when selecting an item neither the bg color or text color changes, even though I've defined an item with android:state_selected="true" in the selector.

Edit: I'm using SDK level 11 (Android 3.0) and a Motorola Xoom.

The list fragment layout:

<?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="fill_parent"
    android:orientation="vertical">
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>

The list item layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="25dp"
    android:background="@drawable/list_item_bg_selector">
    <TextView android:id="@+id/form_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="@dimen/text_size_xlarge"
        android:textStyle="bold"
        android:textColor="@drawable/list_item_text_selector" />
    <TextView android:id="@+id/form_subtitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="@dimen/text_size_medium"
        android:textStyle="normal"
        android:layout_marginTop="5dp"
        android:textColor="@drawable/list_item_text_selector" />
</LinearLayout>

The background selector:

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

The text selector:

<?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/white" />
    <item 
        android:drawable="@color/list_text_blue" />
</selector>

解决方案

The answer is to use the android:state_activated="true" state, instead of the "selected" state. More on this here: ListFragment Item Selected Background