Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Checkable View which responds to Selector

Tags:

I have a group of FrameLayout which I want to be checkable/selectable,

That is, after a click I would like the FrameLayout to display as checked - when pressed again I would like it to become unchecked. What's more, I want this visual que to be described as usual through though the use of a <selector>.

I can't seem to get this working - I'm not sure what I'm missing:

public class CheckableFrameLayout extends FrameLayout implements Checkable {     private boolean mChecked = false;     public CheckableFrameLayout(Context context, AttributeSet attrs) {         super(context, attrs);     }      @Override     public void setChecked(boolean checked) {         mChecked = checked;         refreshDrawableState();     }      public boolean isChecked() {         return mChecked;     }      public void toggle() {         setChecked(!mChecked);     } } 

The layout of the CheckableFrameLayout:

<com.test.view.CheckableFrameLayout     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@drawable/selector_horizontal"     android:clickable="true" > 

The selector backing it (selector_horizontal.xml):

<item android:drawable="@drawable/selector_vertical_selected" android:state_pressed="false" android:state_checked="true"/>   <item android:drawable="@drawable/selector_vertical_pressed" android:state_pressed="true" android:state_checked="false"/> <item android:drawable="@drawable/selector_vertical_normal" android:state_pressed="false" android:state_checked="false"/> 

Using the above code the "state_pressed" is working fine, but the View itself is not becoming checked (not is the Checkable code being called as discovered through debug).

like image 340
Graeme Avatar asked Jul 02 '12 12:07

Graeme


1 Answers

Adding the following code to a Checkable class allows Selectors to work:

private static final int[] CheckedStateSet = {     android.R.attr.state_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(); } 
like image 128
Graeme Avatar answered Sep 21 '22 07:09

Graeme