ListView
カスタムCheckable
リストアイテムのレイアウトがあります。ListView
モードで動作し、チェックされた親アイテムの子ビューCHOICE_MODE_MULTIPLE
を確認したい。どうすればいいですか?CheckBox
com.domain.CheckedRelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<com.domain.CheckedRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"
android:background="@drawable/selector">
<RelativeLayout
android:id="@+id/rl_data"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:paddingTop="2dp"
android:paddingBottom="2dp">
...
</RelativeLayout>
<CheckBox android:id="@+id/cb_select"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="6dp"
android:text="" />
</com.domain.CheckedRelativeLayout>
Java クラス:
public class CheckedRelativeLayout extends RelativeLayout implements Checkable {
private boolean mChecked;
private static final int[] CheckedStateSet = {
android.R.attr.state_checked
};
public CheckedRelativeLayout(Context context) {
super(context);
}
public CheckedRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean isChecked() {
Log.v(getClass().getName(), " IS CHECKED ");
return mChecked;
}
public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
refreshDrawableState();
}
}
@Override
public void toggle() {
mChecked = !mChecked;
Log.v(getClass().getName(), " TOGGLE ");
}
@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();
}
}