CheckableLinearLayout背景确实会随着背景选择器而改变
问题描述:
iv此时至少有一天在这个问题上挣扎,在互联网上搜索,但没有解决方案为我工作,我一直试图理解它为什么会发生,以及如何解决它正确的方法。CheckableLinearLayout背景确实会随着背景选择器而改变
问题: 我无法改变行背景色/绘制时,它被“选中”,我知道什么时候被选中,因为我的根元素实现经过接口。
我不知道设置属性机器人之间的差别:与的Android listSelector ListView控件的:背景我行根元素(CheckableLinearLayout)的财产。
Android会使用选择器并应用我的背景属性?因为他们都没有工作到现在,它总是透明的。
在某些情况下,当我点击列表中的项目,该项目去红色,然后回到透明,也是方法onCreateDrawableState永远不会真的叫,我想可能是相关的。
我的场景:
API 10(2.3.3)使用延伸ArrayAdapter定制适配器一个ListView
一种活性。 我的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">
...
CheckableLinearLayout:
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>
我甚至试图用一个空选择在那里设置背景红色,但仍然,我排队红色。
任何帮助非常感谢!
谢谢!
答
没关系,上面的代码是正确的,我发现在我的代码的另一个地方的以前的尝试中,我将我的列表项背景设置为透明。
删除该代码后,我将我的listview listSelector设置为一个透明选择器(以删除橙色选择背景)。
然后将CheckableLinearLayout背景设置为上面的我的选择器。
之后,我的onCreateDrawableState得到正常调用,并且背景更改如选择器中指定。
就是这样!