setItemChecked没有在Gingerbread上工作

hea*_*des 0 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="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>
Run Code Online (Sandbox Code Playgroud)

整个选择器在Android(ICS,JB)的更高版本上运行良好,但在Gingerbread上,而在_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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用于设置选择器的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"
        />
Run Code Online (Sandbox Code Playgroud)

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

hea*_*des 7

经过一点点搜索之后,在我看来state_checked之前没有表达的原因是Honeycomb 之前的setActive方法View是在API级别11之前不可用.这意味着检查状态不会传播到我的子视图布局.

钥匙:

  1. 交换TextView一个CheckedTextView
  2. 将已检查状态从父视图传播到子视图

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();
    }
}
Run Code Online (Sandbox Code Playgroud)