I am having some very strange ListView behavior when using a StateListDrawable as the background. I've tried to follow the answer to this post, as I wasn't getting the state_checked state to work, but now my ListView is going crazy.
When I click on an item, it doesn't immediately change color to the state_checked item in the selector. After clicking around a bit though, many of the views will suddenly switch to the state_checked background. It's seemingly random.
Here is my state selector xml code:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="@color/grey"
android:endColor="@color/darkgrey"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/grey05" />
<corners
android:radius="0dp" />
<padding
android:left="10sp"
android:top="10sp"
android:right="10sp"
android:bottom="10sp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="@color/orange4"
android:startColor="@color/orange5"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/grey05" />
<corners
android:radius="0dp" />
<padding
android:left="10sp"
android:top="10sp"
android:right="10sp"
android:bottom="10sp" />
</shape>
</item>
<item android:state_checked="true">
<shape>
<gradient
android:endColor="@color/brown2"
android:startColor="@color/brown1"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/grey05" />
<corners
android:radius="0dp" />
<padding
android:left="10sp"
android:top="10sp"
android:right="10sp"
android:bottom="10sp" />
</shape>
</item>
<item android:state_selected="true">
<shape>
<gradient
android:endColor="@color/brown2"
android:startColor="@color/brown1"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/grey05" />
<corners
android:radius="0dp" />
<padding
android:left="10sp"
android:top="10sp"
android:right="10sp"
android:bottom="10sp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="@color/white"
android:endColor="@color/white2"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/grey05" />
<corners
android:radius="0dp" />
<padding
android:left="10sp"
android:top="10sp"
android:right="10sp"
android:bottom="10sp" />
</shape>
</item>
</selector>
And here is my .java class for my Custom view implementing checkable:
public class Entry extends LinearLayout implements Checkable {
public Entry(Context context) {
super(context, null);
// Inflate this view
LayoutInflater temp = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
temp.inflate(R.layout.entry, this, true);
initViews();
}
private static final int[] CheckedStateSet = {
android.R.attr.state_checked
};
private void initViews() {
this.setBackgroundResource(R.drawable.listview_row);
}
public boolean isChecked() {
return _checked;
}
public void toggle() {
_checked = !_checked;
}
public void setChecked(boolean checked) {
_checked = checked;
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CheckedStateSet);
}
return drawableState;
}
@Override
public boolean performClick() {
toggle();
return super.performClick();
}
}
I've poked around for a few hours trying to figure it out, but unfortunately must concede to asking for help. Can anyone see something wrong with the code above that would cause the ListView to behave strangely on the items? I can post more code as well, if needed.