2013-06-12 91 views
0

工作,我现在用的是下面的选择来改变文本的外观在ListView项目:setItemChecked不是姜饼

<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 pressed的项目被正确应用,当我在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版本上进行过测试,但我会尽快编辑这篇文章。

+0

看看这个问题:http://stackoverflow.com/questions/3742979/how-to-get-a-android-listview-item-selector-to-use-state-checked – Voicu

+0

感谢您的提示,但这似乎并没有工作... – sleeke

+0

对不起,这么快就把你的想法拍下来Voicu - 事实证明它让我到一个解决方案(下) – sleeke

回答

7

一点点搜索后,在我看来,之所以说state_checked不表达预蜂窝的事实是,在ViewsetActive方法不是API级别11.这意味着之前可用的选中状态不传播到我的布局的子视图。

THE KEY:

  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(); 
    } 
} 
+1

R.attr.state_checked定义在哪里?它应该在我自己的包中,还是真的是android.R.attr.state_checked属性?如果需要在我自己的包中,查看您的导入或相关的attr.xml文件会很有帮助。 – Baron

+1

@Baron:恐怕既然问了这么长时间以来,我已经不再记得这段代码在哪里使用了,并且无法为你找到import或xml文件,但我几乎可以肯定,按照你的想法引用android.R.attr.state_checked。 虽然进口的好点。我一定会把它们包含在未来的问题中! – sleeke

+0

@sleeke将来的问题:是的,使用的属性是“android.R.attr state_checked。”。测试和完美工作。谢谢! –