0

I have ListView with custom Checkable list item layout. My ListView works in CHOICE_MODE_MULTIPLE mode and I want to check child CheckBox view of cheked parent com.domain.CheckedRelativeLayout item. How can I do it?

<?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 class:

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();
    }

}
IgorOK
  • 1,652
  • 4
  • 18
  • 36
  • Rather than store the boolean value of whether the Checkable should be checked, store a reference to the checkable: [CheckedTextView checkmark in ListView row not showing up](http://stackoverflow.com/q/12125792/1267661) – Sam Oct 04 '12 at 18:26

1 Answers1

1

Since you are defining children in XML, I would just iterate through all the children in the CheckedRelativeLayout and if they implement Checkable, call setChecked on them and pass in the checked state for the CheckedRelativeLayout.

If you were to build the layout in code instead (meaning you guarantee that the children are always there) you could just toggle the checkbox when the state of CheckedRelativeLayout changes.

toadzky
  • 3,806
  • 1
  • 15
  • 26