Browse Source

Dynamic height of collapsing react header (#730)

* Dynamic height of collapsing react header

* Add missing class after rebase
Guy Carmeli 8 years ago
parent
commit
a26f201139

+ 1
- 1
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java View File

@@ -98,7 +98,7 @@ public class TopBar extends AppBarLayout {
98 98
 
99 99
     public TopTabs initTabs() {
100 100
         topTabs = new TopTabs(getContext());
101
-        addView(topTabs);
101
+        addView(topTabs, new ViewGroup.LayoutParams(MATCH_PARENT, (int) ViewUtils.convertDpToPixel(48)));
102 102
         return topTabs;
103 103
     }
104 104
 

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

@@ -0,0 +1,22 @@
1
+package com.reactnativenavigation.views.collapsingToolbar;
2
+
3
+import android.view.ViewGroup;
4
+
5
+import com.reactnativenavigation.views.utils.ViewMeasurer;
6
+
7
+class CollapsingReactHeaderMeasurer extends ViewMeasurer {
8
+    private ViewGroup header;
9
+
10
+    CollapsingReactHeaderMeasurer(ViewGroup header) {
11
+        this.header = header;
12
+    }
13
+
14
+    @Override
15
+    public int getMeasuredHeight(int heightMeasureSpec) {
16
+        return hasChildren() ? header.getChildAt(0).getMeasuredHeight() : super.getMeasuredHeight(heightMeasureSpec);
17
+    }
18
+
19
+    private boolean hasChildren() {
20
+        return header.getChildCount() > 0;
21
+    }
22
+}

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

@@ -10,6 +10,7 @@ import android.widget.ScrollView;
10 10
 import com.reactnativenavigation.params.CollapsingTopBarParams;
11 11
 import com.reactnativenavigation.params.NavigationParams;
12 12
 import com.reactnativenavigation.params.StyleParams;
13
+import com.reactnativenavigation.screens.Screen;
13 14
 import com.reactnativenavigation.utils.ViewUtils;
14 15
 import com.reactnativenavigation.views.TitleBar;
15 16
 import com.reactnativenavigation.views.TopBar;
@@ -77,9 +78,15 @@ public class CollapsingTopBar extends TopBar implements CollapsingView {
77 78
             header = new CollapsingTopBarReactHeader(getContext(),
78 79
                     params,
79 80
                     new NavigationParams(Bundle.EMPTY),
80
-                    scrollListener);
81
-            LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, (int) ViewUtils.convertDpToPixel(params.reactViewHeight));
82
-            titleBarAndContextualMenuContainer.addView(header, lp);
81
+                    scrollListener,
82
+                    new Screen.OnDisplayListener() {
83
+                        @Override
84
+                        public void onDisplay() {
85
+                            calculateFinalCollapsedTranslation();
86
+                            header.setOnDisplayListener(null);
87
+                        }
88
+                    });
89
+            titleBarAndContextualMenuContainer.addView(header, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
83 90
             header.setOnHiddenListener(new CollapsingTopBarReactHeaderAnimator.OnHiddenListener() {
84 91
                 @Override
85 92
                 public void onHidden() {
@@ -169,4 +176,8 @@ public class CollapsingTopBar extends TopBar implements CollapsingView {
169 176
     public View asView() {
170 177
         return this;
171 178
     }
179
+
180
+    public int getTitleBarHeight() {
181
+        return titleBar.getHeight();
182
+    }
172 183
 }

+ 24
- 8
android/app/src/main/java/com/reactnativenavigation/views/collapsingToolbar/CollapsingTopBarReactHeader.java View File

@@ -12,11 +12,13 @@ import com.facebook.react.uimanager.events.EventDispatcher;
12 12
 import com.reactnativenavigation.NavigationApplication;
13 13
 import com.reactnativenavigation.params.CollapsingTopBarParams;
14 14
 import com.reactnativenavigation.params.NavigationParams;
15
+import com.reactnativenavigation.screens.Screen;
15 16
 import com.reactnativenavigation.utils.ViewUtils;
16 17
 import com.reactnativenavigation.views.ContentView;
17 18
 
18
-public class CollapsingTopBarReactHeader extends ContentView implements CollapsingTopBarReactHeaderAnimator.OnVisibleListener, CollapsingTopBarReactHeaderAnimator.OnHiddenListener {
19
+public class CollapsingTopBarReactHeader extends ContentView implements CollapsingTopBarReactHeaderAnimator.OnVisibleListener, CollapsingTopBarReactHeaderAnimator.OnHiddenListener, Screen.OnDisplayListener {
19 20
     private ScrollListener listener;
21
+    private Screen.OnDisplayListener onDisplayListener;
20 22
     private boolean mIsScrolling;
21 23
     private int mTouchSlop;
22 24
     private int touchDown = -1;
@@ -33,19 +35,33 @@ public class CollapsingTopBarReactHeader extends ContentView implements Collapsi
33 35
         this.onHiddenListener = onHiddenListener;
34 36
     }
35 37
 
36
-    public CollapsingTopBarReactHeader(Context context, CollapsingTopBarParams params, NavigationParams navigationParams, ScrollListener scrollListener) {
38
+    public CollapsingTopBarReactHeader(Context context, CollapsingTopBarParams params, NavigationParams navigationParams, ScrollListener scrollListener, Screen.OnDisplayListener onDisplayListener) {
37 39
         super(context, params.reactViewId, navigationParams);
38 40
         listener = scrollListener;
41
+        this.onDisplayListener = onDisplayListener;
39 42
         ViewConfiguration vc = ViewConfiguration.get(context);
40 43
         mTouchSlop = vc.getScaledTouchSlop();
41
-        createVisibilityAnimator(params.reactViewHeight);
44
+        setViewMeasurer(new CollapsingReactHeaderMeasurer(this));
45
+        setOnDisplayListener(this);
42 46
     }
43 47
 
44
-    private void createVisibilityAnimator(int reactViewHeight) {
45
-        float height = ViewUtils.convertDpToPixel(reactViewHeight);
46
-        visibilityAnimator = new CollapsingTopBarReactHeaderAnimator(this, height * 0.6f, height * 0.60f);
47
-        visibilityAnimator.setOnHiddenListener(this);
48
-        visibilityAnimator.setOnVisibleListener(this);
48
+    @Override
49
+    public void onDisplay() {
50
+        createVisibilityAnimator();
51
+        onDisplayListener.onDisplay();
52
+    }
53
+
54
+    private void createVisibilityAnimator() {
55
+        ViewUtils.runOnPreDraw(this, new Runnable() {
56
+            @Override
57
+            public void run() {
58
+                final CollapsingTopBarReactHeader header = CollapsingTopBarReactHeader.this;
59
+                float height = getHeight();
60
+                visibilityAnimator = new CollapsingTopBarReactHeaderAnimator(header, height * 0.6f, height * 0.60f);
61
+                visibilityAnimator.setOnHiddenListener(header);
62
+                visibilityAnimator.setOnVisibleListener(header);
63
+            }
64
+        });
49 65
     }
50 66
 
51 67
     public void collapse(float amount) {

+ 5
- 13
android/app/src/main/java/com/reactnativenavigation/views/collapsingToolbar/CollapsingViewMeasurer.java View File

@@ -6,38 +6,30 @@ import com.reactnativenavigation.utils.ViewUtils;
6 6
 import com.reactnativenavigation.views.utils.ViewMeasurer;
7 7
 
8 8
 public class CollapsingViewMeasurer extends ViewMeasurer {
9
-    int collapsedTopBarHeight;
10
-    private float finalCollapseValue;
11 9
     int screenHeight;
12 10
     int bottomTabsHeight = 0;
11
+    CollapsingTopBar topBar;
13 12
     protected StyleParams styleParams;
14 13
 
15 14
     public CollapsingViewMeasurer(final CollapsingTopBar topBar, final Screen collapsingSingleScreen, StyleParams styleParams) {
15
+        this.topBar = topBar;
16 16
         this.styleParams = styleParams;
17 17
         bottomTabsHeight = (int) ViewUtils.convertDpToPixel(56);
18
-        ViewUtils.runOnPreDraw(topBar, new Runnable() {
19
-            @Override
20
-            public void run() {
21
-                collapsedTopBarHeight = topBar.getCollapsedHeight();
22
-                finalCollapseValue = topBar.getFinalCollapseValue();
23
-            }
24
-        });
25
-
26 18
         ViewUtils.runOnPreDraw(collapsingSingleScreen, new Runnable() {
27 19
             @Override
28 20
             public void run() {
29
-                screenHeight = collapsingSingleScreen.getMeasuredHeight();
21
+                screenHeight = collapsingSingleScreen.getHeight();
30 22
             }
31 23
         });
32 24
     }
33 25
 
34 26
     public float getFinalCollapseValue() {
35
-        return finalCollapseValue;
27
+        return topBar.getFinalCollapseValue();
36 28
     }
37 29
 
38 30
     @Override
39 31
     public int getMeasuredHeight(int heightMeasureSpec) {
40
-        int height = screenHeight - collapsedTopBarHeight;
32
+        int height = screenHeight - topBar.getCollapsedHeight();
41 33
         if (styleParams.bottomTabsHidden) {
42 34
             height += bottomTabsHeight;
43 35
         }

+ 18
- 4
android/app/src/main/java/com/reactnativenavigation/views/collapsingToolbar/CollapsingViewPagerContentViewMeasurer.java View File

@@ -2,19 +2,33 @@ package com.reactnativenavigation.views.collapsingToolbar;
2 2
 
3 3
 import com.reactnativenavigation.params.StyleParams;
4 4
 import com.reactnativenavigation.screens.Screen;
5
+import com.reactnativenavigation.utils.ViewUtils;
5 6
 
6 7
 public class CollapsingViewPagerContentViewMeasurer extends CollapsingViewMeasurer {
8
+    private int titleBarHeight;
7 9
 
8
-    public CollapsingViewPagerContentViewMeasurer(CollapsingTopBar topBar, Screen screen, StyleParams styleParams) {
10
+    public CollapsingViewPagerContentViewMeasurer(final CollapsingTopBar topBar, Screen screen, StyleParams styleParams) {
9 11
         super(topBar, screen, styleParams);
12
+        ViewUtils.runOnPreDraw(topBar, new Runnable() {
13
+            @Override
14
+            public void run() {
15
+                titleBarHeight = topBar.getTitleBarHeight();
16
+            }
17
+        });
10 18
     }
11 19
 
12 20
     @Override
13 21
     public int getMeasuredHeight(int heightMeasureSpec) {
14
-        int height = screenHeight - collapsedTopBarHeight;
15
-//        if (styleParams.bottomTabsHidden) {
22
+        int height = screenHeight - topBar.getCollapsedHeight();
23
+        if (styleParams.bottomTabsHidden) {
16 24
             height -= bottomTabsHeight;
17
-//        }
25
+        }
26
+        if (!styleParams.titleBarHideOnScroll) {
27
+            height -= titleBarHeight;
28
+        }
29
+        if (!styleParams.drawScreenAboveBottomTabs) {
30
+            height -= bottomTabsHeight;
31
+        }
18 32
         return height;
19 33
     }
20 34
 }