Yeah this code is...  wrong.  I don't know what checkedItemIndex is, but you
aren't supposed to look up a particular index to find an int... the array is
a set of ints.  And you can't retrieve the array and modify it; nobody else
will know that you modified it (if you even modify it correctly at all,
which this doesn't).

Here is the code of interest from CompoundButton that adds its checked state
to the state set:

       private boolean mChecked;


    private static final int[] CHECKED_STATE_SET = {

        R.attr.state_checked

    };

       public boolean isChecked() {

        return mChecked;

    }


    public void setChecked(boolean checked) {

        if (mChecked != checked) {

            mChecked = checked;

            refreshDrawableState();


            // ...

        }

    }


       @Override

    protected int[] onCreateDrawableState(int extraSpace) {

        final int[] drawableState = super.onCreateDrawableState(extraSpace +
1);

        if (isChecked()) {

            mergeDrawableStates(drawableState, CHECKED_STATE_SET);

        }

        return drawableState;

    }

Also any state you set in to ImageView will be merged in to the state set,
because it does do the appropriate thing in onCreateDrawableState():

       @Override

    public int[] onCreateDrawableState(int extraSpace) {

        if (mState == null) {

            return super.onCreateDrawableState(extraSpace);

        } else if (!mMergeState) {

            return mState;

        } else {

            return mergeDrawableStates(

                    super.onCreateDrawableState(extraSpace + mState.length),
mState);

        }

    }

And the function you call to set the additional state keeps hold of it and
ensures the current drawables are updated with it:

       public void setImageState(int[] state, boolean merge) {

        mState = state;

        mMergeState = merge;

        if (mDrawable != null) {

            refreshDrawableState();

            resizeFromDrawable();

        }

    }

(Though actually I see there is a small bug here -- if you haven't set the
image view's Drawable it should still do refreshDrawableState() to ensure
any background drawing gets updated.)

On Tue, Nov 16, 2010 at 9:28 AM, Bret Foreman <bret.fore...@gmail.com>wrote:

> Leaving aside the lamentable wording of the documentation for this
> method, it's not really acting as I hoped for an ImageView. I call
> toggle() from the onClick method of the view but the view system does
> not redraw my drawable with the checked state. However, if I
> initialize  CHECKED_STATE_SET with android.R.attr.state_checked then
> the view is draw with the checked state.
>
> So apparently my call to setImageState is not updating the state of
> the ImageView. The documentation for setImageState is totally blank in
> the case of an ImageView. What does this mean?
>
> Here's my code:
>
>        private static final int[] CHECKED_STATE_SET =
> {android.R.attr.state_empty};
>
>                @Override
>                public boolean isChecked() {
>                        int[] ds = getDrawableState();
>                        return ds[checkedItemIndex] ==
> android.R.attr.state_checked;
>                }
>
>                @Override
>                public void setChecked(boolean isChecked) {
>                        int[] ds = getDrawableState();
>                        if( isChecked ) {
>
>  ds[checkedItemIndex]=android.R.attr.state_checked;
>                        } else {
>
>  ds[checkedItemIndex]=android.R.attr.state_empty;
>                        }
>                        setImageState(ds,false);
>                        refreshDrawableState();
>                }
>
>                @Override
>                public void toggle() {
>                        setChecked( !isChecked() );
>                }
>
>                @Override
>                public int[] onCreateDrawableState(int extraSpace) {
>                        int[] drawableState =
> super.onCreateDrawableState(extraSpace +
> CHECKED_STATE_SET.length );
>                        checkedItemIndex = drawableState.length -
> CHECKED_STATE_SET.length;
>                        mergeDrawableStates( drawableState ,
> CHECKED_STATE_SET );
>                        return drawableState;
>                }
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to