我有一个 ListView(单选模式),其中包含 CheckedTextView 项(没有复选标记),并且我设置了适当的选择器来确定当前选中项的文本颜色和背景颜色。
会发生以下情况:
- 我按下 ListView 的一个项目:“选定”状态的颜色(黑色超过红色)得到正确应用。
- 我释放了该项目:它突然闪回到“默认”状态的颜色(白黑),持续几分之一秒。
- 在短暂的错误闪烁后:该项目正确地采用了“选中”状态的颜色(黑色比红色,偶然地与“选中”状态相同)。
当然,我需要摆脱这种闪烁效果,我认为这应该很容易,但不幸的是,我不知道我做错了什么! p>
请注意,错误的闪烁效果只发生在 Honeycomb 之前的 Android 版本上(我已经在模拟器和物理设备上检查过这一点)。
我已经在整个网络和 Stack Overflow 上搜索了答案,所以请不要向我发送现有的类似帖子(当然,除非它们真的解决了我的问题......)。
在下面,您可以找到重现上述问题所需的所有代码。
activity(没错,是兼容库的一个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 :
<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>
高亮背景的drawable (highlighted_item_background.xml):
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff0000" />
</shape>
普通背景的drawable(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>
基本上我们默认选择高亮显示,并且只有在项目被未选中时才将其设置为正常。
关于android - ListView 内的 CheckedTextView : blinking issue upon selection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9893760/