【问题标题】:ListView Item Selected State not workingListView 项目选定状态不起作用
【发布时间】:2014-11-17 18:26:20
【问题描述】:

所以我有一个 ListView,我想更改每个项目背景和文本的颜色。此 ListView 位于 ListFragment 内。我的代码扩充了onCreateView 中的布局,并扩充了newView 中每个项目的布局。

android:state_pressed="true" 工作正常,每当我按下一个项目时,背景就会变为该颜色。 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.

编辑:我使用的是 SDK level 11 (Android 3.0) 和 Motorola Xoom。

列表片段布局:

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

列表项布局:

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

背景选择器:

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

文本选择器:

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

【问题讨论】:

  • @Snicolas 我不认为这个问题与这个问题有关,我什至没有使用半透明颜色

标签: android listview android-layout


【解决方案1】:

答案是使用android:state_activated="true" 状态,而不是“选定”状态。更多信息在这里:ListFragment Item Selected Background

【讨论】:

  • 你能在这里检查我的问题吗,我想你可以回答它:) stackoverflow.com/questions/14644517/…Tahnks
  • 对我不起作用。你能发布一个用于各州的 XML 可绘制示例吗?
  • 不适用于 API v10 和以下版本。请参阅我的帖子以提供更好的解决方案。
【解决方案2】:

支持所有 API 级别的最佳解决方案是为列表项 View 实现 Checkable 功能,这意味着列表项布局的顶部视图必须实现 Checkable 接口(在我的情况下是 TextView,但是同样可以应用于ViewGroup 类,如LinearLayout)。当您单击列表项时,ListView 调用 setChecked 方法,我们将视图的状态更改为使用 android:state_checked="true" 选择器。与列表视图android:choiceMode="singleChoice" 一起,它将只选择一项。

诀窍是覆盖onCreateDrawableState 方法并在此处为可绘制对象设置检查状态。请参阅下面的SelectableTextView 示例。调用setChecked 后,存储检查状态并调用refreshDrawableState

SelectableTextView 的示例:

package com.example.widget.SelectableTextView;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.TextView;

public class SelectableTextView extends TextView implements Checkable {
    private static final int[] CHECKED_STATE_SET = {
                    android.R.attr.state_checked
    };

    private boolean mChecked;

    public SelectableTextView(Context context) {
        super(context);
    }

    public SelectableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SelectableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SelectableTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            refreshDrawableState();
        }
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void toggle() {
        setSelected(!mChecked);
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }
}

selectable_list_item.xml 布局示例:

<?xml version="1.0" encoding="utf-8"?>
<com.example.widget.SelectableTextView xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@android:id/text1"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:textColor="@color/list_item_selector_foreground"
          android:background="@drawable/list_item_selector_background"
          tools:text="Item 1"/>

list_item_selector_foreground.xml 的示例:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- checked -->
    <item android:color="@color/list_item_text_active" android:state_checked="true"/>

    <item android:color="@color/list_item_text"/>
</selector>

list_item_selector_background.xml 的示例:

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

    <item android:drawable="@color/list_item_background"/>
</selector>

【讨论】:

  • 这对我有用.. 简单而正确的方法
【解决方案3】:

不要忘记为布局设置clickable="true"。这解决了我的问题。

列表项布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/list_item_bg_selector"
        android:clickable="true" >

        <TextView
            android:id="@+id/tvNewsPreviewTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="3"
            android:ellipsize="end"
            android:textSize="@dimen/news_preview_title_textsize"
            android:textStyle="bold" />
    </RelativeLayout>

背景选择器:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" >
            <shape android:shape="rectangle">
                <stroke android:width="1dp" android:color="@color/black" />
                <gradient android:startColor="@color/white" android:endColor="@color/white" />
            </shape>
        </item>
        <item>
            <shape android:shape="rectangle">
                <stroke android:width="1dp" android:color="@color/holo_gray_darker" />
                <gradient android:startColor="@color/holo_gray_bright" android:endColor="@color/holo_gray_bright" />
            </shape>
        </item>
    </selector>

【讨论】:

    【解决方案4】:

    @Andrew S:除了在选择器中使用激活状态外,默认情况下,激活状态必须设置为 false,如下面的选择器代码所示。

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_pressed="false"
            android:state_activated="false"
            android:color="@color/dark_text_blue"/>
      <item android:state_pressed="true"
            android:color="@color/red"/>
      <item android:state_activated="true"
            android:color="@color/red"/>
    </selector>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-01
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      相关资源
      最近更新 更多