CheckedTextView라는 뷰가 있습니다. 이 뷰를 참고하셔서 만드시면 될거 같은데요
실제 코드 내부를 보면 Checkable를 implement해서 구현됐던거로 기억을 하는데요
체크 기능을 가진 뷰로 재정의 하셔서 사용하시면 될거 같습니다
예전에 제가 썻던 방식입니다...
최상위 뷰는 부조건 Check기능을 가진 레이아웃이여야 하며
하위 뷰에도 Check (on/off)값을 넘겨 줘야 하위 뷰에도 적용 됩니다.
public class CheckedRelativeLayout extends RelativeLayout implements Checkable {
private boolean mChecked;
// private int mCheckMarkResource;
private Drawable mCheckMarkDrawable;
// private int mBasePaddingRight;
// private int mCheckMarkWidth;
private static final int[] CHECKED_STATE_SET = {
R.attr.state_checked
};
public CheckedRelativeLayout(Context context) {
this(context, null);
}
public CheckedRelativeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CheckedRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setChecked(false);
}
public void toggle() {
setChecked(!mChecked);
}
public boolean isChecked() {
return mChecked;
}
/**
* <p>Changes the checked state of this text view.</p>
*
* @param checked true to check the text, false to uncheck it
*/
public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
int count = getChildCount();
for(int i = 0; i < count; i++){
if(getChildAt(i).getClass().getSimpleName().equalsIgnoreCase("CheckedTextView") ){
CheckedTextView checkedTextView = (CheckedTextView)getChildAt(i);
checkedTextView.setChecked(checked);
}else if(getChildAt(i).getClass().getSimpleName().equalsIgnoreCase("CheckedRelativeLayout") ){
CheckedRelativeLayout checkedRelativeLayout = (CheckedRelativeLayout)getChildAt(i);
checkedRelativeLayout.setChecked(checked);
}else if(getChildAt(i).getClass().getSimpleName().equalsIgnoreCase("CheckedLinearLayout") ){
CheckedRelativeLayout checkedRelativeLayout = (CheckedRelativeLayout)getChildAt(i);
checkedRelativeLayout.setChecked(checked);
}else if(getChildAt(i).getClass().getSimpleName().equalsIgnoreCase("CheckBox") ){
CheckBox checkbox = (CheckBox)getChildAt(i);
checkbox.setChecked(checked);
}
}
refreshDrawableState();
}
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mCheckMarkDrawable != null) {
int[] myDrawableState = getDrawableState();
// Set the state of the Drawable
mCheckMarkDrawable.setState(myDrawableState);
invalidate();
}
}
@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
boolean populated = super.dispatchPopulateAccessibilityEvent(event);
if (!populated) {
event.setChecked(mChecked);
}
return super.dispatchPopulateAccessibilityEvent(event);
}
}