Browse Source

More work towards collapsing react view in top bar

This commit contains various fixes and enhancements, mostly because I
was too lazy to commit small changes.

* Add topBarCollapseOnScroll style property which compliments
  navBarHideOnScroll. This property is needed when showing a
  collapsing topTabs screen. In this case the developer might to keep
  the titleBar bar when the screen is collapsed.
* Reduce collapse sensitivity by taking scaled touch slop into account
* Cancel touches when collapsing. This fix prevents buttons from
  responding to touch events when collapse ends.
Guy Carmeli 7 years ago
parent
commit
2391fd0ba9

+ 1
- 0
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java View File

@@ -40,6 +40,7 @@ public class StyleParams {
40 40
 
41 41
     public Color topBarColor;
42 42
     public CollapsingTopBarParams collapsingTopBarParams;
43
+    public boolean topBarCollapseOnScroll;
43 44
     public boolean topBarHidden;
44 45
     public boolean topBarElevationShadowEnabled;
45 46
     public boolean topTabsHidden;

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

@@ -23,7 +23,7 @@ class CollapsingTopBarParamsParser extends Parser {
23 23
 
24 24
     public CollapsingTopBarParams parse() {
25 25
         if (!validateParams()) {
26
-            return null;
26
+            return new CollapsingTopBarParams();
27 27
         }
28 28
         CollapsingTopBarParams result = new CollapsingTopBarParams();
29 29
         result.imageUri = params.getString("collapsingToolBarImage", null);

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

@@ -27,6 +27,7 @@ public class StyleParamsParser {
27 27
         result.topBarColor = getColor("topBarColor", getDefaultTopBarColor());
28 28
         result.titleBarHideOnScroll = getBoolean("titleBarHideOnScroll", getDefaultTitleBarHideOnScroll());
29 29
         result.topBarTransparent = getBoolean("topBarTransparent", getDefaultTopBarHidden());
30
+        result.topBarCollapseOnScroll = getBoolean("topBarCollapseOnScroll", false);
30 31
         result.drawScreenBelowTopBar = params.getBoolean("drawBelowTopBar", getDefaultScreenBelowTopBar());
31 32
         if (result.topBarTransparent) {
32 33
             result.drawScreenBelowTopBar = false;

+ 5
- 2
android/app/src/main/java/com/reactnativenavigation/screens/CollapsingSingleScreen.java View File

@@ -1,8 +1,10 @@
1 1
 package com.reactnativenavigation.screens;
2 2
 
3 3
 import android.support.v7.app.AppCompatActivity;
4
+import android.view.MotionEvent;
4 5
 import android.widget.ScrollView;
5 6
 
7
+import com.facebook.react.uimanager.RootViewUtil;
6 8
 import com.reactnativenavigation.params.ScreenParams;
7 9
 import com.reactnativenavigation.views.CollapsingContentView;
8 10
 import com.reactnativenavigation.views.LeftButtonOnClickListener;
@@ -31,7 +33,7 @@ public class CollapsingSingleScreen extends SingleScreen {
31 33
 
32 34
     @Override
33 35
     protected TopBar createTopBar() {
34
-        final CollapsingTopBar topBar = new CollapsingTopBar(getContext(), styleParams.collapsingTopBarParams);
36
+        final CollapsingTopBar topBar = new CollapsingTopBar(getContext(), styleParams);
35 37
         topBar.setScrollListener(getScrollListener(topBar));
36 38
         return topBar;
37 39
     }
@@ -63,8 +65,9 @@ public class CollapsingSingleScreen extends SingleScreen {
63 65
         return new ScrollListener(new CollapseCalculator(topBar, getCollapseBehaviour()),
64 66
                 new OnScrollListener() {
65 67
                     @Override
66
-                    public void onScroll(CollapseAmount amount) {
68
+                    public void onScroll(MotionEvent event, CollapseAmount amount) {
67 69
                         if (screenParams.styleParams.drawScreenBelowTopBar) {
70
+                            RootViewUtil.getRootView(contentView).onChildStartedNativeGesture(event);
68 71
                             ((CollapsingView) contentView).collapse(amount);
69 72
                         }
70 73
                         topBar.collapse(amount);

+ 5
- 2
android/app/src/main/java/com/reactnativenavigation/screens/CollapsingViewPagerScreen.java View File

@@ -4,8 +4,10 @@ import android.annotation.SuppressLint;
4 4
 import android.content.Context;
5 5
 import android.support.v4.view.ViewPager;
6 6
 import android.support.v7.app.AppCompatActivity;
7
+import android.view.MotionEvent;
7 8
 import android.widget.ScrollView;
8 9
 
10
+import com.facebook.react.uimanager.RootViewUtil;
9 11
 import com.reactnativenavigation.events.Event;
10 12
 import com.reactnativenavigation.events.ViewPagerScreenChangedEvent;
11 13
 import com.reactnativenavigation.events.ViewPagerScreenScrollStartEvent;
@@ -35,7 +37,7 @@ public class CollapsingViewPagerScreen extends ViewPagerScreen {
35 37
 
36 38
     @Override
37 39
     protected TopBar createTopBar() {
38
-        final CollapsingTopBar topBar = new CollapsingTopBar(getContext(), styleParams.collapsingTopBarParams);
40
+        final CollapsingTopBar topBar = new CollapsingTopBar(getContext(), styleParams);
39 41
         topBar.setScrollListener(getScrollListener(topBar));
40 42
         return topBar;
41 43
     }
@@ -70,7 +72,8 @@ public class CollapsingViewPagerScreen extends ViewPagerScreen {
70 72
         return new ScrollListener(new CollapseCalculator(topBar, getCollapseBehaviour()),
71 73
                 new OnScrollListener() {
72 74
                     @Override
73
-                    public void onScroll(CollapseAmount amount) {
75
+                    public void onScroll(MotionEvent event, CollapseAmount amount) {
76
+                        RootViewUtil.getRootView(getCurrentPage()).onChildStartedNativeGesture(event);
74 77
                         topBar.collapse(amount);
75 78
                         ((CollapsingView) viewPager).collapse(amount);
76 79
                     }

+ 2
- 1
android/app/src/main/java/com/reactnativenavigation/views/collapsingToolbar/CollapseCalculator.java View File

@@ -194,7 +194,8 @@ public class CollapseCalculator {
194 194
         totalCollapse += collapse;
195 195
         previousCollapseY = y;
196 196
         previousTouchEvent = MotionEvent.obtain(event);
197
-        return Math.abs(totalCollapse) < scaledTouchSlop ? CollapseAmount.None : new CollapseAmount(collapse);
197
+        final float delta = Math.abs(y - touchDownY);
198
+        return Math.abs(delta) < scaledTouchSlop ? CollapseAmount.None : new CollapseAmount(collapse);
198 199
     }
199 200
 
200 201
     private float calculateCollapse(float y) {

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

@@ -9,6 +9,7 @@ import android.widget.ScrollView;
9 9
 
10 10
 import com.reactnativenavigation.params.CollapsingTopBarParams;
11 11
 import com.reactnativenavigation.params.NavigationParams;
12
+import com.reactnativenavigation.params.StyleParams;
12 13
 import com.reactnativenavigation.utils.ViewUtils;
13 14
 import com.reactnativenavigation.views.TitleBar;
14 15
 import com.reactnativenavigation.views.TopBar;
@@ -21,26 +22,31 @@ public class CollapsingTopBar extends TopBar implements CollapsingView {
21 22
     private CollapsingTopBarReactHeader header;
22 23
     private ScrollListener scrollListener;
23 24
     private float finalCollapsedTranslation;
24
-    private CollapsingTopBarParams params;
25
+    private final StyleParams styleParams;
26
+    private final CollapsingTopBarParams params;
25 27
     private final ViewCollapser viewCollapser;
26 28
     private final int topBarHeight;
27 29
     private String title;
28 30
 
29
-    public CollapsingTopBar(Context context, final CollapsingTopBarParams params) {
31
+    public CollapsingTopBar(Context context, final StyleParams params) {
30 32
         super(context);
31
-        this.params = params;
33
+        styleParams = params;
34
+        this.params = params.collapsingTopBarParams;
32 35
         topBarHeight = calculateTopBarHeight();
33
-        createBackgroundImage(params);
34
-        calculateFinalCollapsedTranslation(params);
36
+        createBackgroundImage();
37
+        calculateFinalCollapsedTranslation();
35 38
         viewCollapser = new ViewCollapser(this);
36 39
     }
37 40
 
38
-    private void calculateFinalCollapsedTranslation(final CollapsingTopBarParams params) {
41
+    private void calculateFinalCollapsedTranslation() {
39 42
         ViewUtils.runOnPreDraw(this, new Runnable() {
40 43
             @Override
41 44
             public void run() {
42 45
                 if (params.hasBackgroundImage() || params.hasReactView()) {
43 46
                     finalCollapsedTranslation = getCollapsedHeight() - getHeight();
47
+                    if (styleParams.topBarCollapseOnScroll) {
48
+                        finalCollapsedTranslation += titleBar.getHeight();
49
+                    }
44 50
                 } else {
45 51
                     finalCollapsedTranslation = -titleBar.getHeight();
46 52
                 }
@@ -61,7 +67,7 @@ public class CollapsingTopBar extends TopBar implements CollapsingView {
61 67
         }
62 68
     }
63 69
 
64
-    private void createBackgroundImage(CollapsingTopBarParams params) {
70
+    private void createBackgroundImage() {
65 71
         if (params.hasBackgroundImage()) {
66 72
             collapsingTopBarBackground = new CollapsingTopBarBackground(getContext(), params);
67 73
             LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, (int) CollapsingTopBarBackground.MAX_HEIGHT);

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

@@ -1,5 +1,7 @@
1 1
 package com.reactnativenavigation.views.collapsingToolbar;
2 2
 
3
+import android.view.MotionEvent;
4
+
3 5
 public interface  OnScrollListener extends OnFlingListener {
4
-    void onScroll(CollapseAmount amount);
6
+    void onScroll(MotionEvent event, CollapseAmount amount);
5 7
 }

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

@@ -27,7 +27,7 @@ public class ScrollListener {
27 27
     boolean onTouch(MotionEvent event) {
28 28
         CollapseAmount amount = collapseCalculator.calculate(event);
29 29
         if (amount.canCollapse()) {
30
-            scrollListener.onScroll(amount);
30
+            scrollListener.onScroll(event, amount);
31 31
             return collapseBehaviour instanceof CollapseTopBarBehaviour;
32 32
         }
33 33
         return false;

+ 1
- 0
src/deprecated/platformSpecificDeprecated.android.js View File

@@ -125,6 +125,7 @@ function convertStyleParams(originalStyleObject) {
125 125
     topBarTransparent: originalStyleObject.navBarTransparent,
126 126
     topBarTranslucent: originalStyleObject.navBarTranslucent,
127 127
     topBarElevationShadowEnabled: originalStyleObject.topBarElevationShadowEnabled,
128
+    topBarCollapseOnScroll: originalStyleObject.topBarCollapseOnScroll,
128 129
     collapsingToolBarImage: originalStyleObject.collapsingToolBarImage,
129 130
     collapsingToolBarComponent: originalStyleObject.collapsingToolBarComponent,
130 131
     collapsingToolBarComponentHeight: originalStyleObject.collapsingToolBarComponentHeight,