Setitemchecked Not Working On Gingerbread
I am using the following selector to change the appearance of text in a listView item: -
Solution 1:
After a little searching, it seems to me the reason that the state_checked
isn't expressed pre-Honeycomb is the fact that the setActive
method on View
is not available before API level 11. This means that the checked state is not propagated to the child views of my layout.
THE KEY:
- Swap the
TextView
for aCheckedTextView
- Propagate the checked state from the parent view to the children
1) Was a simple switch in the XML, and for 2) I modified the code in the answer linked to by Voicu to give the following:
publicclassCheckableRelativeLayoutextendsRelativeLayoutimplementsCheckable
{
privatebooleanchecked=false;
publicCheckableRelativeLayout(Context context) {
super(context, null);
}
publicCheckableRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
privatestaticfinalint[] CheckedStateSet = {
R.attr.state_checked
};
@OverrideprotectedvoiddispatchSetPressed(boolean pressed)
{
super.dispatchSetPressed(pressed);
setChecked(pressed);
}
@OverridepublicvoidsetChecked(boolean checked) {
this.checked = checked;
for (intindex=0; index < getChildCount(); index++)
{
Viewview= getChildAt(index);
if (view.getClass().toString().equals(CheckedTextView.class.toString()))
{
CheckedTextViewcheckable= (CheckedTextView)view;
checkable.setChecked(checked);
checkable.refreshDrawableState();
}
}
refreshDrawableState();
}
publicbooleanisChecked() {
return checked;
}
publicvoidtoggle() {
checked = !checked;
refreshDrawableState();
}
@Overrideprotectedint[] onCreateDrawableState(int extraSpace) {
finalint[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CheckedStateSet);
}
return drawableState;
}
@OverridepublicbooleanperformClick() {
returnsuper.performClick();
}
}
Post a Comment for "Setitemchecked Not Working On Gingerbread"