到目前为止,我一直在为这个问题苦苦挣扎至少一天,在互联网上搜索,但没有任何解决方案对我有用,我一直试图了解它为什么会发生,以及如何以正确的方式解决它。
问题:当它被“选中”时,我无法更改行背景颜色/可绘制对象,我知道它何时被选中,因为我的根元素实现了Checked接口。
我不知道设置ListView的属性android:listSelector和我的行根元素 (CheckableLinearLayout)的android:background属性之间的区别。
Android 将使用选择器并应用我的背景的哪些属性?因为直到现在它们都没有工作,所以它总是透明的。
在某些情况下,当我点击列表项时,该项目变为红色,然后又变回透明,而且onCreateDrawableState方法也从未真正被调用,我认为它可能是相关的。
我的场景:
API 10 (2.3.3)
一个带有 ListView 的 Activity,使用扩展 ArrayAdapter 的自定义适配器。我的 ListView 行资源根元素:
<com.company.views.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:orientation="horizontal">
...
可检查线性布局:
public class CheckableLinearLayout extends LinearLayout implements Checkable {
private static final int[] STATE_CHECKED = {android.R.attr.state_checked};
@Override
protected int[] onCreateDrawableState(int extraSpace)
{
...
int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked())
mergeDrawableStates(drawableState, STATE_CHECKED);
return drawableState;
}
...
}
我的选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape>
<solid android:color="@color/solid_blue" />
</shape>
</item>
<item>
<shape>
<solid android:color="@color/solid_red" />
</shape>
</item>
</selector>
我什至尝试使用一个空选择器将背景设置为红色,但我的行仍然是红色的。
任何帮助深表感谢!
谢谢!