Browse Source

Support adding ReactView in CollapsingTopBar

Guy Carmeli 8 years ago
parent
commit
d4ad120ebc

+ 1
- 1
android/app/src/main/java/com/reactnativenavigation/params/parsers/CollapsingTopBarParamsParser.java View File

@@ -42,7 +42,7 @@ class CollapsingTopBarParamsParser extends Parser {
42 42
     }
43 43
 
44 44
     private CollapseBehaviour getCollapseBehaviour() {
45
-        if (hasBackgroundImage()) {
45
+        if (hasBackgroundImage() || hasReactView()) {
46 46
             return new CollapseTopBarBehaviour();
47 47
         }
48 48
         if (titleBarHideOnScroll && drawBelowTopBar) {

+ 32
- 10
android/app/src/main/java/com/reactnativenavigation/views/collapsingToolbar/CollapsingTopBar.java View File

@@ -1,25 +1,32 @@
1 1
 package com.reactnativenavigation.views.collapsingToolbar;
2 2
 
3 3
 import android.content.Context;
4
+import android.content.res.TypedArray;
5
+import android.os.Bundle;
4 6
 import android.view.View;
5 7
 import android.widget.ScrollView;
6 8
 
7 9
 import com.reactnativenavigation.params.CollapsingTopBarParams;
10
+import com.reactnativenavigation.params.NavigationParams;
8 11
 import com.reactnativenavigation.utils.ViewUtils;
9 12
 import com.reactnativenavigation.views.TitleBar;
10 13
 import com.reactnativenavigation.views.TopBar;
11 14
 
12 15
 public class CollapsingTopBar extends TopBar implements CollapsingView {
13 16
     private CollapsingTopBarBackground collapsingTopBarBackground;
17
+    private CollapsingTopBarReactView headerView;
14 18
     private ScrollListener scrollListener;
15 19
     private float finalCollapsedTranslation;
16 20
     private CollapsingTopBarParams params;
17 21
     private final ViewCollapser viewCollapser;
22
+    private final int topBarHeight;
18 23
 
19 24
     public CollapsingTopBar(Context context, final CollapsingTopBarParams params) {
20 25
         super(context);
21 26
         this.params = params;
22
-        createCollapsingTopBar(params);
27
+        topBarHeight = calculateTopBarHeight();
28
+        createBackgroundImage(params);
29
+        createReactView(params);
23 30
         calculateFinalCollapsedTranslation(params);
24 31
         viewCollapser = new ViewCollapser(this);
25 32
     }
@@ -29,8 +36,7 @@ public class CollapsingTopBar extends TopBar implements CollapsingView {
29 36
             @Override
30 37
             public void run() {
31 38
                 if (params.hasBackgroundImage()) {
32
-                    finalCollapsedTranslation =
33
-                            getCollapsingTopBarBackground().getCollapsedTopBarHeight() - getHeight();
39
+                    finalCollapsedTranslation = getCollapsedHeight() - getHeight();
34 40
                 } else {
35 41
                     finalCollapsedTranslation = -titleBar.getHeight();
36 42
                 }
@@ -42,7 +48,7 @@ public class CollapsingTopBar extends TopBar implements CollapsingView {
42 48
         this.scrollListener = scrollListener;
43 49
     }
44 50
 
45
-    private void createCollapsingTopBar(CollapsingTopBarParams params) {
51
+    private void createBackgroundImage(CollapsingTopBarParams params) {
46 52
         if (params.hasBackgroundImage()) {
47 53
             collapsingTopBarBackground = new CollapsingTopBarBackground(getContext(), params);
48 54
             LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, (int) CollapsingTopBarBackground.MAX_HEIGHT);
@@ -50,21 +56,27 @@ public class CollapsingTopBar extends TopBar implements CollapsingView {
50 56
         }
51 57
     }
52 58
 
59
+    private void createReactView(CollapsingTopBarParams params) {
60
+        if (params.hasReactView()) {
61
+            headerView = new CollapsingTopBarReactView(getContext(),
62
+                    params.reactViewId,
63
+                    new NavigationParams(Bundle.EMPTY));
64
+            LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, (int) ViewUtils.convertDpToPixel(400));
65
+            titleBarAndContextualMenuContainer.addView(headerView, lp);
66
+        }
67
+    }
68
+
53 69
     @Override
54 70
     protected TitleBar createTitleBar() {
55 71
         if (params.hasBackgroundImage()) {
56 72
             return new CollapsingTitleBar(getContext(),
57
-                    collapsingTopBarBackground.getCollapsedTopBarHeight(),
73
+                    getCollapsedHeight(),
58 74
                     scrollListener);
59 75
         } else {
60 76
             return super.createTitleBar();
61 77
         }
62 78
     }
63 79
 
64
-    public CollapsingTopBarBackground getCollapsingTopBarBackground() {
65
-        return collapsingTopBarBackground;
66
-    }
67
-
68 80
     @Override
69 81
     public void collapse(CollapseAmount amount) {
70 82
         viewCollapser.collapse(amount);
@@ -87,7 +99,9 @@ public class CollapsingTopBar extends TopBar implements CollapsingView {
87 99
 
88 100
     public int getCollapsedHeight() {
89 101
         if (params.hasBackgroundImage()) {
90
-            return collapsingTopBarBackground.getCollapsedTopBarHeight();
102
+            return topBarHeight;
103
+        } else if (params.hasReactView()) {
104
+            return topBarHeight;
91 105
         } else if (topTabs != null) {
92 106
             return topTabs.getHeight();
93 107
         } else {
@@ -95,6 +109,14 @@ public class CollapsingTopBar extends TopBar implements CollapsingView {
95 109
         }
96 110
     }
97 111
 
112
+    private int calculateTopBarHeight() {
113
+        int[] attrs = new int[] {android.R.attr.actionBarSize};
114
+        TypedArray ta = getContext().obtainStyledAttributes(attrs);
115
+        final int result = ta.getDimensionPixelSize(0, -1);
116
+        ta.recycle();
117
+        return result;
118
+    }
119
+
98 120
     @Override
99 121
     public float getCurrentCollapseValue() {
100 122
         return getTranslationY();

+ 0
- 16
android/app/src/main/java/com/reactnativenavigation/views/collapsingToolbar/CollapsingTopBarBackground.java View File

@@ -1,7 +1,6 @@
1 1
 package com.reactnativenavigation.views.collapsingToolbar;
2 2
 
3 3
 import android.content.Context;
4
-import android.content.res.TypedArray;
5 4
 import android.widget.FrameLayout;
6 5
 import android.widget.ImageView;
7 6
 
@@ -17,7 +16,6 @@ public class CollapsingTopBarBackground extends FrameLayout {
17 16
     private final CollapsingTopBarParams params;
18 17
     private SimpleDraweeView backdrop;
19 18
     private Scrim scrim;
20
-    private int topBarHeight = -1;
21 19
 
22 20
     public CollapsingTopBarBackground(Context context, CollapsingTopBarParams params) {
23 21
         super(context);
@@ -47,20 +45,6 @@ public class CollapsingTopBarBackground extends FrameLayout {
47 45
         addView(scrim);
48 46
     }
49 47
 
50
-    public int getCollapsedTopBarHeight() {
51
-        if (topBarHeight > -1) {
52
-            return topBarHeight;
53
-        }
54
-        calculateTopBarHeight();
55
-        return topBarHeight;
56
-    }
57
-    private void calculateTopBarHeight() {
58
-        int[] attrs = new int[] {android.R.attr.actionBarSize};
59
-        TypedArray ta = getContext().obtainStyledAttributes(attrs);
60
-        topBarHeight = ta.getDimensionPixelSize(0, -1);
61
-        ta.recycle();
62
-    }
63
-
64 48
     public void collapse(float collapse) {
65 49
         scrim.collapse(collapse);
66 50
     }

+ 12
- 0
android/app/src/main/java/com/reactnativenavigation/views/collapsingToolbar/CollapsingTopBarReactView.java View File

@@ -0,0 +1,12 @@
1
+package com.reactnativenavigation.views.collapsingToolbar;
2
+
3
+import android.content.Context;
4
+
5
+import com.reactnativenavigation.params.NavigationParams;
6
+import com.reactnativenavigation.views.ContentView;
7
+
8
+public class CollapsingTopBarReactView extends ContentView {
9
+    public CollapsingTopBarReactView(Context context, String screenId, NavigationParams navigationParams) {
10
+        super(context, screenId, navigationParams);
11
+    }
12
+}