Reputation: 41
Recently,I am using the widget named switch. The switch is default widget of Android Studio.The problem is that when I add new Item with switch,its animation will run but I don't want to see it. I used ListView and Cursor Adapter in my project.And the switch is a item as an element of ListView. Do you know how to solve it?
Upvotes: 4
Views: 3967
Reputation: 402
If you're having unwanted animation, when Switch
is displayed for the 1st time, then it may help you to use SwitchCompat
instead.
Upvotes: 0
Reputation: 1875
This is a piece of code from Switch widget (you can see it too - just press Ctrl and click Switch
in your Android Studio project window):
@Override
public void setChecked(boolean checked) {
super.setChecked(checked);
// Calling the super method may result in setChecked() getting called
// recursively with a different value, so load the REAL value...
checked = isChecked();
if (getWindowToken() != null && ViewCompat.isLaidOut(this) && isShown()) {
animateThumbToCheckedState(checked);
} else {
// Immediately move the thumb to the new position.
cancelPositionAnimator();
setThumbPosition(checked ? 1 : 0);
}
}
It will always show animation when visible. So you can't disable it.
But you can extend CompoundButton
class and make your own switch widget without any animation.
Also you can copy code from Switch widget, rename it, remove animation, make some drawables and you'll get a non-animated switch.
Upvotes: 1
Reputation: 364
Calling jumpDrawablesToCurrentState()
will disable the animation, if you call it immediately after calling setChecked()
on the switch.
Upvotes: 10
Reputation: 1847
I was able to disable the animation of the switch by removing it from the view hierarchy, calling setChecked(val) while it's removed, and then readding it to the view hierarchy.
It's kludgey, but as far as I can tell it's the only way to do it -- you can't subclass Switch to override the setChecked() behavior because you still need to call super.setChecked() which will run Switch#setChecked() and do the animation anyway.
Upvotes: -1