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
 
98
 
99
     public TopTabs initTabs() {
99
     public TopTabs initTabs() {
100
         topTabs = new TopTabs(getContext());
100
         topTabs = new TopTabs(getContext());
101
-        addView(topTabs);
101
+        addView(topTabs, new ViewGroup.LayoutParams(MATCH_PARENT, (int) ViewUtils.convertDpToPixel(48)));
102
         return topTabs;
102
         return topTabs;
103
     }
103
     }
104
 
104
 

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

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
 import com.reactnativenavigation.params.CollapsingTopBarParams;
10
 import com.reactnativenavigation.params.CollapsingTopBarParams;
11
 import com.reactnativenavigation.params.NavigationParams;
11
 import com.reactnativenavigation.params.NavigationParams;
12
 import com.reactnativenavigation.params.StyleParams;
12
 import com.reactnativenavigation.params.StyleParams;
13
+import com.reactnativenavigation.screens.Screen;
13
 import com.reactnativenavigation.utils.ViewUtils;
14
 import com.reactnativenavigation.utils.ViewUtils;
14
 import com.reactnativenavigation.views.TitleBar;
15
 import com.reactnativenavigation.views.TitleBar;
15
 import com.reactnativenavigation.views.TopBar;
16
 import com.reactnativenavigation.views.TopBar;
77
             header = new CollapsingTopBarReactHeader(getContext(),
78
             header = new CollapsingTopBarReactHeader(getContext(),
78
                     params,
79
                     params,
79
                     new NavigationParams(Bundle.EMPTY),
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
             header.setOnHiddenListener(new CollapsingTopBarReactHeaderAnimator.OnHiddenListener() {
90
             header.setOnHiddenListener(new CollapsingTopBarReactHeaderAnimator.OnHiddenListener() {
84
                 @Override
91
                 @Override
85
                 public void onHidden() {
92
                 public void onHidden() {
169
     public View asView() {
176
     public View asView() {
170
         return this;
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
 import com.reactnativenavigation.NavigationApplication;
12
 import com.reactnativenavigation.NavigationApplication;
13
 import com.reactnativenavigation.params.CollapsingTopBarParams;
13
 import com.reactnativenavigation.params.CollapsingTopBarParams;
14
 import com.reactnativenavigation.params.NavigationParams;
14
 import com.reactnativenavigation.params.NavigationParams;
15
+import com.reactnativenavigation.screens.Screen;
15
 import com.reactnativenavigation.utils.ViewUtils;
16
 import com.reactnativenavigation.utils.ViewUtils;
16
 import com.reactnativenavigation.views.ContentView;
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
     private ScrollListener listener;
20
     private ScrollListener listener;
21
+    private Screen.OnDisplayListener onDisplayListener;
20
     private boolean mIsScrolling;
22
     private boolean mIsScrolling;
21
     private int mTouchSlop;
23
     private int mTouchSlop;
22
     private int touchDown = -1;
24
     private int touchDown = -1;
33
         this.onHiddenListener = onHiddenListener;
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
         super(context, params.reactViewId, navigationParams);
39
         super(context, params.reactViewId, navigationParams);
38
         listener = scrollListener;
40
         listener = scrollListener;
41
+        this.onDisplayListener = onDisplayListener;
39
         ViewConfiguration vc = ViewConfiguration.get(context);
42
         ViewConfiguration vc = ViewConfiguration.get(context);
40
         mTouchSlop = vc.getScaledTouchSlop();
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
     public void collapse(float amount) {
67
     public void collapse(float amount) {

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

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

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

2
 
2
 
3
 import com.reactnativenavigation.params.StyleParams;
3
 import com.reactnativenavigation.params.StyleParams;
4
 import com.reactnativenavigation.screens.Screen;
4
 import com.reactnativenavigation.screens.Screen;
5
+import com.reactnativenavigation.utils.ViewUtils;
5
 
6
 
6
 public class CollapsingViewPagerContentViewMeasurer extends CollapsingViewMeasurer {
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
         super(topBar, screen, styleParams);
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
     @Override
20
     @Override
13
     public int getMeasuredHeight(int heightMeasureSpec) {
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
             height -= bottomTabsHeight;
24
             height -= bottomTabsHeight;
17
-//        }
25
+        }
26
+        if (!styleParams.titleBarHideOnScroll) {
27
+            height -= titleBarHeight;
28
+        }
29
+        if (!styleParams.drawScreenAboveBottomTabs) {
30
+            height -= bottomTabsHeight;
31
+        }
18
         return height;
32
         return height;
19
     }
33
     }
20
 }
34
 }