소스 검색

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

+ 0
- 6
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java 파일 보기

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