我有一个ListView(单选模式),其中包含CheckedTextView项(没有复选标记),并且我设置了适当的选择器来确定当前选中项的文本颜色和背景颜色。
发生的情况如下:
- 我按下ListView的一项:正确应用"选定"状态的颜色(黑色高于红色)
- 我发布了这个项目:它突然闪烁回"默认"状态的颜色(白色比黑色),持续了几分之一秒
- 在短暂的错误闪烁之后:项目正确地采用了"已检查"状态的颜色(黑色高于红色,这很可能与"已选择"状态相同)
当然,我需要摆脱这种眨眼的效果,我认为这应该很容易,但不幸的是,我不知道我做错了什么!
请注意,错误的闪烁效果只发生在Honeycomb之前的Android版本上(我在模拟器和物理设备上都检查过)。
我已经搜索了整个网络和整个Stack Overflow来找到答案,所以请不要向我介绍现有的类似帖子(当然,除非他们真的解决了我的问题…)
在下面的内容中,您可以找到重现我上面描述的问题所需的所有代码。
活动(是的,它是兼容库的FragmentActivity,但我真的不认为它重要):
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TempListActivity extends FragmentActivity{
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.temp_list);
ListView listView = (ListView) findViewById(R.id.tempListView);
String[] array = new String[] { "one", "two", "three" };
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.temp_list_item, array);
listView.setAdapter(arrayAdapter);
}
}
列表视图:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tempListView"
android:choiceMode="singleChoice"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
用作ListView项的CheckedTextView:
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_title"
android:height="?android:attr/listPreferredItemHeight"
android:textColor="@drawable/list_item_text_custom"
android:background="@drawable/list_item_custom"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
文本颜色选择器(list_item_text_custom.xml):
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:color="#000000">
</item>
<item android:state_pressed="true"
android:color="#000000">
</item>
<item android:state_selected="true"
android:color="#000000">
</item>
<item
android:color="#ffffff">
</item>
</selector>
背景颜色选择器(list_item_custom.xml):
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/highlighted_item_background">
</item>
<item android:state_pressed="true"
android:drawable="@drawable/highlighted_item_background">
</item>
<item android:state_selected="true"
android:drawable="@drawable/highlighted_item_background">
</item>
<item
android:drawable="@drawable/normal_item_background">
</item>
</selector>
突出显示背景的绘图(highlighted_item_background.xml):
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff0000" />
</shape>
正常背景的绘图(normal_item_background.xml):
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#000000" />
</shape>
我真的很想利用标准的Android选择器机制,而不是被迫找到一些花哨的变通方法,所以如果我在上面的代码中犯了任何错误,请告诉我。
否则,我们将非常感谢任何帮助或替代解决方案。
提前谢谢。
我一直无法为您找到真正的解决方案,但如果我们简化您所拥有的,也许我们可以在您想要的和您需要的之间找到可以接受的折衷方案:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@drawable/normal_item_background">
</item>
<item
android:drawable="@drawable/highlighted_item_background">
</item>
</selector>
基本上,我们在默认情况下保持选择高亮显示,只有当项目未选中时才将其设置为正常。