浏览代码

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

Also cleaned up VisibilityAnimator.
Guy Carmeli 7 年前
父节点
当前提交
7919e65932

+ 21
- 36
android/app/src/main/java/com/reactnativenavigation/animation/VisibilityAnimator.java 查看文件

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

+ 0
- 6
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java 查看文件

@@ -32,11 +32,6 @@ 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
-
40 35
     @Override
41 36
     public void onViewAdded(View child) {
42 37
         super.onViewAdded(child);
@@ -76,7 +71,6 @@ public class TitleBar extends Toolbar {
76 71
     }
77 72
 
78 73
     public void setStyle(StyleParams params) {
79
-        setVisibility(params.titleBarHidden ? GONE : VISIBLE);
80 74
         setTitleTextColor(params);
81 75
         setTitleTextFont(params);
82 76
         setSubtitleTextColor(params);