2

I have a ListView where each item is a CheckBox. I keep the state of each item in an ArrayList to make sure that the recycled views are not mixed up.

The only problem with that is that I have to use setChecked on each CheckBox row item, which at first shows checked then shows the unchecking animation.

Is there a way to force setChecked on a CheckBox without the animation? Instantly, I mean.

Alex Ferg
  • 801
  • 1
  • 9
  • 17
  • 1
    If you set it when it's not attached to a window, then there will be no animation. See http://stackoverflow.com/questions/27139262/change-switch-state-without-animation for more info. – lionscribe Aug 09 '16 at 04:39
  • This worked for me. Thanks! – Alex Ferg Aug 09 '16 at 14:25

2 Answers2

0

One of the ways you can do this is to use your own check images in the CheckBox. You'll need images for the different states which you can obtain from http://android-holo-colors.com/

Then we can reference these drawables in our code as:

    Drawable mEnabledBg = getResources().getDrawable(R.drawable.check_bg_enabled);
    Drawable mCheckedBg = getResources().getDrawable(R.drawable.check_bg_checked);
    Drawable mDisabledBg = getResources().getDrawable(R.drawable.check_bg_disabled);

Now, we use them in a StateListDrawbles:

    StateListDrawable tickColorStates = new StateListDrawable();

    tickColorStates.addState(new int[]{android.R.attr.state_checked}, mCheckedBg); //checked
    tickColorStates.addState(new int[]{-android.R.attr.state_checked, android.R.attr.state_enabled}, mEnabledBg); //un-checked
    tickColorStates.addState(new int[]{-android.R.attr.state_enabled}, mDisabledBg); //disabled

    checkBox.setButtonDrawable(tickColorStates);

Of course this setup can also be done in xml and you'll simply need to call setButtonDrawable() then. Hope this helps.

Shaishav
  • 5,282
  • 2
  • 22
  • 41
0

You don't need to implement your own drawables. Just use the method jumpDrawablesToCurrentState() like this:

private fun setChecked(
    btn: CompoundButton,
    checked: Boolean,
    withTransition: Boolean
) {
    btn.isChecked = checked

    if (!withTransition) {
        btn.jumpDrawablesToCurrentState()
    }
}
digory doo
  • 1,978
  • 2
  • 23
  • 37