我有ListView
自定义Checkable
列表项布局。我ListView
在CHOICE_MODE_MULTIPLE
模式下工作,我想检查CheckBox
cheked 父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();
}
}