Parcourir la source

Revert "Fix title bar not displayed if it was initially hidden (#1341)" (#1342)

This reverts commit 7919e65932.
Guy Carmeli il y a 7 ans
Parent
révision
8fd1a231ad

+ 36
- 21
android/app/src/main/java/com/reactnativenavigation/animation/VisibilityAnimator.java Voir le fichier

@@ -1,23 +1,27 @@
1 1
 package com.reactnativenavigation.animation;
2 2
 
3
+import android.animation.Animator;
4
+import android.animation.AnimatorListenerAdapter;
3 5
 import android.animation.ObjectAnimator;
4 6
 import android.support.v4.view.animation.LinearOutSlowInInterpolator;
5 7
 import android.view.View;
6 8
 
7 9
 public class VisibilityAnimator {
8 10
 
9
-    private final LinearOutSlowInInterpolator interpolator = new LinearOutSlowInInterpolator();
10
-    private ObjectAnimator animator;
11
-
12 11
     public enum HideDirection {
13 12
         Up, Down
14 13
     }
15 14
 
15
+    private enum VisibilityState {
16
+        Hidden, AnimateHide, Shown, AnimateShow
17
+    }
18
+
16 19
     private static final int SHOW_END_VALUE = 0;
17 20
     private static final int DURATION = 300;
18 21
 
19 22
     private final View view;
20 23
     private final int hiddenEndValue;
24
+    private VisibilityState visibilityState = VisibilityState.Shown;
21 25
 
22 26
     public VisibilityAnimator(View view, HideDirection hideDirection, int height) {
23 27
         this.view = view;
@@ -25,45 +29,56 @@ public class VisibilityAnimator {
25 29
     }
26 30
 
27 31
     public void setVisible(boolean visible, boolean animate) {
28
-        cancelAnimator();
29
-        if (visible) {
32
+        if (visible && isHiding()) {
30 33
             show(animate);
31
-        } else {
34
+        } else if (!visible && isShowing()) {
32 35
             hide(animate);
33 36
         }
34 37
     }
35 38
 
36
-    private void cancelAnimator() {
37
-        if (animator != null && animator.isRunning()) {
38
-            view.clearAnimation();
39
-            animator.cancel();
40
-        }
41
-    }
42
-
43 39
     private void show(boolean animate) {
44 40
         if (animate) {
45
-            animator = createAnimator(true);
41
+            ObjectAnimator animator = createAnimator(true);
46 42
             animator.start();
47 43
         } else {
48
-            view.setTranslationY(SHOW_END_VALUE);
49
-            view.setY(SHOW_END_VALUE);
44
+            visibilityState = VisibilityState.Shown;
45
+            view.setVisibility(View.VISIBLE);
50 46
         }
51 47
     }
52 48
 
53 49
     private void hide(boolean animate) {
54 50
         if (animate) {
55
-            animator = createAnimator(false);
51
+            ObjectAnimator animator = createAnimator(false);
56 52
             animator.start();
57 53
         } else {
58
-            view.setTranslationY(hiddenEndValue);
59
-            view.setY(hiddenEndValue);
54
+            visibilityState = VisibilityState.Hidden;
55
+            view.setVisibility(View.GONE);
60 56
         }
61 57
     }
62 58
 
59
+    private boolean isShowing() {
60
+        return visibilityState == VisibilityState.Shown || visibilityState == VisibilityState.AnimateShow;
61
+    }
62
+
63
+    private boolean isHiding() {
64
+        return visibilityState == VisibilityState.Hidden || visibilityState == VisibilityState.AnimateHide;
65
+    }
66
+
63 67
     private ObjectAnimator createAnimator(final boolean show) {
64
-        final ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, show ? SHOW_END_VALUE : hiddenEndValue);
68
+        ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, show ? SHOW_END_VALUE : hiddenEndValue);
65 69
         animator.setDuration(DURATION);
66
-        animator.setInterpolator(interpolator);
70
+        animator.setInterpolator(new LinearOutSlowInInterpolator());
71
+        animator.addListener(new AnimatorListenerAdapter() {
72
+            @Override
73
+            public void onAnimationStart(Animator animation) {
74
+                visibilityState = show ? VisibilityState.AnimateShow : VisibilityState.AnimateHide;
75
+            }
76
+
77
+            @Override
78
+            public void onAnimationEnd(Animator animation) {
79
+                visibilityState = show ? VisibilityState.Shown : VisibilityState.Hidden;
80
+            }
81
+        });
67 82
         return animator;
68 83
     }
69 84
 }

+ 6
- 0
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java Voir le fichier

@@ -32,6 +32,11 @@ public class TitleBar extends Toolbar {
32 32
         super(context);
33 33
     }
34 34
 
35
+    @Override
36
+    protected void onLayout(boolean changed, int l, int t, int r, int b) {
37
+        super.onLayout(changed, l, t, r, b);
38
+    }
39
+
35 40
     @Override
36 41
     public void onViewAdded(View child) {
37 42
         super.onViewAdded(child);
@@ -71,6 +76,7 @@ public class TitleBar extends Toolbar {
71 76
     }
72 77
 
73 78
     public void setStyle(StyleParams params) {
79
+        setVisibility(params.titleBarHidden ? GONE : VISIBLE);
74 80
         setTitleTextColor(params);
75 81
         setTitleTextFont(params);
76 82
         setSubtitleTextColor(params);