Prevent multi animators setting property concurrently

It crashes on Android 5.0 (API 21) platform.
Fix topjohnwu#5793
This commit is contained in:
残页 2022-05-31 21:24:20 +08:00 committed by John Wu
parent 8a73a16029
commit 3219d945f5

View File

@ -23,6 +23,7 @@ public class ConcealableBottomNavigationView extends BottomNavigationView {
}; };
private boolean isHidden; private boolean isHidden;
private int lastHeight = -1;
public ConcealableBottomNavigationView(@NonNull Context context) { public ConcealableBottomNavigationView(@NonNull Context context) {
this(context, null); this(context, null);
@ -41,6 +42,14 @@ public class ConcealableBottomNavigationView extends BottomNavigationView {
} }
private void recreateAnimator(int height) { private void recreateAnimator(int height) {
if (lastHeight == height) return;
lastHeight = height;
// End the current animation before setting a new one
// otherwise it crashes on Android 5.0
StateListAnimator lastAnimator = getStateListAnimator();
if (lastAnimator != null) lastAnimator.jumpToCurrentState();
Animator toHidden = ObjectAnimator.ofFloat(this, "translationY", height); Animator toHidden = ObjectAnimator.ofFloat(this, "translationY", height);
toHidden.setDuration(175); toHidden.setDuration(175);
toHidden.setInterpolator(new FastOutLinearInInterpolator()); toHidden.setInterpolator(new FastOutLinearInInterpolator());