Android setItemChecked对姜饼不起作用

Android setItemChecked对姜饼不起作用,android,listview,selector,android-2.3-gingerbread,Android,Listview,Selector,Android 2.3 Gingerbread,我正在使用以下选择器更改listView项目中文本的外观: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:color="#FFFFFFFF" /> <!-- checked --> <item android:state_activated=

我正在使用以下选择器更改listView项目中文本的外观:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
          android:color="#FFFFFFFF" /> <!-- checked -->
    <item android:state_activated="true"
          android:color="#FFFFFFFF" /> <!-- activated -->
    <item android:state_pressed="true"
          android:color="#FFFFFFFF" /> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#FFFFFFFF" /> <!-- focused -->
    <item android:color="#FF000000" /> <!-- default -->
</selector>
整个选择器在更高版本的Android ICS、JB上运行良好,但在姜饼上,当正确应用了pressed_state项时,在listView上调用setItemChecked时,state_checked项不被应用

我用于设置项目的代码如下所示:

@Override
protected void onResume()
{
    super.onResume();

    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    for (int index = 0; index < measureList.size(); index++)
    {
        if (measureList.get(index).getId() == appContext.getMeasureId())
        {
            getListView().setItemChecked(index, true);
        }
    }
}
用于设置选择器的xml如下所示:

<TextView
        android:id="@+id/item_text"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:paddingRight="10dp"
        android:ellipsize="end"
        android:layout_toRightOf="@id/item_thumb"
        android:maxLines="1"
        android:scrollHorizontally="true"
        android:textStyle="bold"
        android:textSize="16sp"
        android:textColor="@color/selected_text_selector"
        />

有人知道为什么会这样吗?我还没有在GB和ICS之间的Android版本上测试过它,但我会尽快编辑这篇文章。

在我看来,在蜂巢之前没有表示检查状态的原因是,在API级别11之前,视图上的setActive方法不可用。这意味着选中状态不会传播到布局的子视图

关键是:

将TextView替换为CheckedTextView 将选中状态从父视图传播到子视图 1是XML中的一个简单开关,对于2,我修改了Voicu链接到的答案中的代码,给出以下内容:

public class CheckableRelativeLayout extends RelativeLayout implements Checkable
{
    private boolean checked = false;

    public CheckableRelativeLayout(Context context) {
        super(context, null);
    }

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

    private static final int[] CheckedStateSet = {
            R.attr.state_checked
    };

    @Override
    protected void dispatchSetPressed(boolean pressed)
    {
        super.dispatchSetPressed(pressed);
        setChecked(pressed);
    }

    @Override
    public void setChecked(boolean checked) {
        this.checked = checked;
        for (int index = 0; index < getChildCount(); index++)
        {
            View view = getChildAt(index);
            if (view.getClass().toString().equals(CheckedTextView.class.toString()))
            {
                CheckedTextView checkable = (CheckedTextView)view;
                checkable.setChecked(checked);
                checkable.refreshDrawableState();
            }
        }
        refreshDrawableState();
    }

    public boolean isChecked() {
        return checked;
    }

    public void toggle() {
        checked = !checked;
        refreshDrawableState();
    }

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

    @Override
    public boolean performClick() {
        return super.performClick();
    }
}

检查这个问题:谢谢你的提示,但这似乎不起作用…很抱歉这么快就把你的想法否决了Voicu-结果让我来看看下面的解决方案R.attr.state\u定义在哪里?它应该在我自己的包中,还是真的是android.R.attr.state\u checked属性?如果需要在我自己的软件包中,查看您的导入或相关的attr.xml文件会很有帮助。@Baron:我恐怕已经很久没有问过这个问题了,我已经记不起这段代码是在哪里使用的,也找不到您的导入或xml文件,但我几乎可以肯定我指的是android.R.attr.state_,正如你所想。不过,关于进口的观点很好。我一定会在以后的问题中包括它们@接下来的问题:是的,使用的属性是android.R.attr state\u checked。。测试和工作完美。非常感谢。