package io.hefuyi.zhihudaily.widget;
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;
/**
* Created by hefuyi on 16/8/2.
*/
public class CheckableLinearLayout extends LinearLayout implements Checkable {
private boolean mChecked;
private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
public CheckableLinearLayout(Context context) {
this(context, null);
}
public CheckableLinearLayout(Context context, AttributeSet attributeSet) {
this(context, attributeSet, 0);
}
public CheckableLinearLayout(Context context, AttributeSet attributeSet, int defStyleAttr) {
super(context, attributeSet, defStyleAttr);
}
/*handle chicks*/
@Override
public boolean performClick() {
toggle();
return super.performClick();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return onTouchEvent(ev);
}
/*checkable*/
@Override
public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
refreshDrawableState();
//使当前viewGroup下的子view设置为checked状态
setCheckedRecursive(this, checked);
}
}
private void setCheckedRecursive(ViewGroup parent, boolean checked) {
int count = parent.getChildCount();
for (int i=0;i<count;i++) {
View v = parent.getChildAt(i);
if (v instanceof Checkable) {
((Checkable) v).setChecked(checked);
}
if (v instanceof ViewGroup) {
setCheckedRecursive((ViewGroup) v, checked);
}
}
}
@Override
public boolean isChecked() {
return mChecked;
}
@Override
public void toggle() {
setChecked(!mChecked);
}
/*drawable states*/
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableStare = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableStare, CHECKED_STATE_SET);
}
return drawableStare;
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
Drawable drawable = getBackground();
if (drawable != null) {
int[] myDrawableStete = getDrawableState();
drawable.setState(myDrawableStete);
invalidate();
}
}
}