Make Relativelayout Checkable
Solution 1:
There seem to be two UI patterns for multi-select lists in Holo (in the context of ActionMode): using checkboxes or highlighting.
If all you need is highlighting, you can use this simpler method:
Make your list item layout use for the root a class like this one:
publicclassCheckableRelativeLayoutextendsRelativeLayoutimplementsCheckable {
privatestaticfinalint[] STATE_CHECKABLE = {R.attr.state_pressed};
booleanchecked=false;
publicvoidsetChecked(boolean checked) {
this.checked = checked;
refreshDrawableState();
}
publicbooleanisChecked() {
return checked;
}
publicvoidtoggle() {
setChecked(!checked);
}
@Overrideprotectedint[] onCreateDrawableState(int extraSpace) {
int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (checked) mergeDrawableStates(drawableState, STATE_CHECKABLE);
return drawableState;
}
}
and make sure the root element in your list item layout uses
android:background="?android:attr/listChoiceBackgroundIndicator"
I know this is not really an answer to your question (you wanted an actual checkbox), but I haven't seen this documented anywhere.
The ListView uses the Checkable interface to determine whether it can rely on the list item element to display the checkable state or if it should try to display it itself.
Solution 2:
I just need override method:
privatestaticfinalint[] STATE_CHECKABLE = {android.R.attr.state_checked};
@Overrideprotectedint[] onCreateDrawableState(int extraSpace)
{
int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked) mergeDrawableStates(drawableState, STATE_CHECKABLE);
return drawableState;
}
Now all works.
Solution 3:
Nik's answer is partially right for me, you need added android.R.attr.state_checkable into the STATE_CHECKABLE array, and set the corresponding lenth (2), or else it fails to work.
privatestaticfinalint[] CHECKED_STATE_SET = {android.R.attr.state_checked, android.R.attr.state_checkable};
@Overrideprotectedint[] onCreateDrawableState(int extraSpace)
{
int[] result = super.onCreateDrawableState(extraSpace + CHECKED_STATE_SET.length);
if(mChecked) mergeDrawableStates(result, CHECKED_STATE_SET);
return result;
}
Solution 4:
http://developer.android.com/reference/android/view/View.html#setClickable%28boolean%29
public MyCheckbox(Context context)
{
this(context, null);
this.setClickable(true);
}
public MyCheckbox(Context context, AttributeSet attrs)
{
super(context, attrs);
this.setClickable(true);
//......
}
Post a Comment for "Make Relativelayout Checkable"