Skip to content

Instantly share code, notes, and snippets.

@dforsyth
Created November 16, 2014 20:08
Show Gist options
  • Save dforsyth/ef4da0c4bb0bb019a6bc to your computer and use it in GitHub Desktop.
Save dforsyth/ef4da0c4bb0bb019a6bc to your computer and use it in GitHub Desktop.
checkablelinearlayout
package org.dforsyth.android.garbage;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.LinearLayout;
public class CheckableLinearLayout extends LinearLayout implements Checkable {
// This is kind of half assed but whatever.
private boolean mChecked;
private OnCheckedChangeListener mOnCheckedChangeListener;
private static final int[] CHECKED_STATE_LIST = {
android.R.attr.state_checked,
android.R.attr.state_selected
};
public CheckableLinearLayout(Context context) {
super(context);
}
public CheckableLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CheckableLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setChecked(boolean checked) {
if (mChecked == checked) {
return;
}
mChecked = checked;
refreshDrawableState();
updateCheckableChildren(this);
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
}
}
private void updateCheckableChildren(ViewGroup parent) {
// this should probably take a checked boolean...
int numChildren = parent.getChildCount();
for(int i = 0; i < numChildren; i++ ) {
View view = parent.getChildAt(i);
if(view instanceof Checkable) {
Checkable checkable = (Checkable) view;
checkable.setChecked(mChecked);
}
if(view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
updateCheckableChildren(group);
}
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return onTouchEvent(ev);
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
int[] drawableState = super.onCreateDrawableState(extraSpace + CHECKED_STATE_LIST.length);
if (mChecked) {
mergeDrawableStates(drawableState, CHECKED_STATE_LIST);
}
return drawableState;
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
Drawable drawable = getBackground();
if (drawable == null) {
return;
}
drawable.setState(getDrawableState());
invalidate();
}
@Override
public boolean isChecked() {
return mChecked;
}
@Override
public void toggle() {
setChecked(!mChecked);
}
@Override
public boolean performClick() {
toggle();
return super.performClick();
}
public interface OnCheckedChangeListener {
public void onCheckedChanged(CheckableLinearLayout layout, boolean checked);
}
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment