Browse Source

Collapse when swiping header

Guy Carmeli 7 years ago
parent
commit
376972dc37

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

@@ -6,6 +6,7 @@ import android.widget.ScrollView;
6 6
 import com.reactnativenavigation.params.ScreenParams;
7 7
 import com.reactnativenavigation.views.CollapsingContentView;
8 8
 import com.reactnativenavigation.views.LeftButtonOnClickListener;
9
+import com.reactnativenavigation.views.TopBar;
9 10
 import com.reactnativenavigation.views.collapsingToolbar.CollapseAmount;
10 11
 import com.reactnativenavigation.views.collapsingToolbar.CollapseCalculator;
11 12
 import com.reactnativenavigation.views.collapsingToolbar.CollapsingTopBar;
@@ -29,10 +30,10 @@ public class CollapsingSingleScreen extends SingleScreen {
29 30
     }
30 31
 
31 32
     @Override
32
-    protected void createTopBar() {
33
+    protected TopBar createTopBar() {
33 34
         final CollapsingTopBar topBar = new CollapsingTopBar(getContext(), styleParams.collapsingTopBarParams);
34 35
         topBar.setScrollListener(getScrollListener(topBar));
35
-        this.topBar = topBar;
36
+        return topBar;
36 37
     }
37 38
 
38 39
     @Override

+ 18
- 16
android/app/src/main/java/com/reactnativenavigation/screens/CollapsingViewPagerScreen.java View File

@@ -14,6 +14,7 @@ import com.reactnativenavigation.params.ScreenParams;
14 14
 import com.reactnativenavigation.views.CollapsingContentView;
15 15
 import com.reactnativenavigation.views.ContentView;
16 16
 import com.reactnativenavigation.views.LeftButtonOnClickListener;
17
+import com.reactnativenavigation.views.TopBar;
17 18
 import com.reactnativenavigation.views.collapsingToolbar.CollapseAmount;
18 19
 import com.reactnativenavigation.views.collapsingToolbar.CollapseCalculator;
19 20
 import com.reactnativenavigation.views.collapsingToolbar.CollapsingTopBar;
@@ -33,10 +34,10 @@ public class CollapsingViewPagerScreen extends ViewPagerScreen {
33 34
     }
34 35
 
35 36
     @Override
36
-    protected void createTopBar() {
37
+    protected TopBar createTopBar() {
37 38
         final CollapsingTopBar topBar = new CollapsingTopBar(getContext(), styleParams.collapsingTopBarParams);
38
-        topBar.setScrollListener(getScrollListener());
39
-        this.topBar = topBar;
39
+        topBar.setScrollListener(getScrollListener(topBar));
40
+        return topBar;
40 41
     }
41 42
 
42 43
     @Override
@@ -49,18 +50,15 @@ public class CollapsingViewPagerScreen extends ViewPagerScreen {
49 50
     }
50 51
 
51 52
     protected ContentView createContentView(PageParams tab) {
52
-        if (tab.hasCollapsingTopBar()) {
53
-            CollapsingContentView contentView = new CollapsingContentView(getContext(), tab.screenId, tab.navigationParams);
54
-            contentView.setViewMeasurer(new CollapsingViewPagerContentViewMeasurer((CollapsingTopBar) topBar, this, screenParams.styleParams));
55
-            setupCollapseDetection(contentView);
56
-            return contentView;
57
-        } else {
58
-            return new ContentView(getContext(), tab.screenId, tab.navigationParams);
59
-        }
53
+        CollapsingContentView contentView = new CollapsingContentView(getContext(), tab.screenId, tab.navigationParams);
54
+        contentView.setViewMeasurer(new CollapsingViewPagerContentViewMeasurer((CollapsingTopBar) topBar, this, screenParams.styleParams));
55
+        setupCollapseDetection(contentView);
56
+        return contentView;
60 57
     }
61 58
 
62 59
     private void setupCollapseDetection(CollapsingContentView contentView) {
63
-        contentView.setupCollapseDetection(getScrollListener(), new OnScrollViewAddedListener() {
60
+        ScrollListener scrollListener = getScrollListener((CollapsingView) topBar);
61
+        contentView.setupCollapseDetection(scrollListener, new OnScrollViewAddedListener() {
64 62
             @Override
65 63
             public void onScrollViewAdded(ScrollView scrollView) {
66 64
                 ((CollapsingTopBar) topBar).onScrollViewAdded(scrollView);
@@ -68,18 +66,18 @@ public class CollapsingViewPagerScreen extends ViewPagerScreen {
68 66
         });
69 67
     }
70 68
 
71
-    private ScrollListener getScrollListener() {
72
-        return new ScrollListener(new CollapseCalculator((CollapsingView) topBar, getCollapseBehaviour()),
69
+    private ScrollListener getScrollListener(final CollapsingView topBar) {
70
+        return new ScrollListener(new CollapseCalculator(topBar, getCollapseBehaviour()),
73 71
                 new OnScrollListener() {
74 72
                     @Override
75 73
                     public void onScroll(CollapseAmount amount) {
76
-                        ((CollapsingView) topBar).collapse(amount);
74
+                        topBar.collapse(amount);
77 75
                         ((CollapsingView) viewPager).collapse(amount);
78 76
                     }
79 77
 
80 78
                     @Override
81 79
                     public void onFling(CollapseAmount amount) {
82
-                        ((CollapsingView) topBar).collapse(amount);
80
+                        topBar.collapse(amount);
83 81
                         ((CollapsingView) viewPager).collapse(amount);
84 82
                     }
85 83
                 },
@@ -111,4 +109,8 @@ public class CollapsingViewPagerScreen extends ViewPagerScreen {
111 109
             }
112 110
         }
113 111
     }
112
+
113
+    protected ContentView getCurrentPage() {
114
+        return (ContentView) viewPager.getChildAt(viewPager.getCurrentItem());
115
+    }
114 116
 }

+ 3
- 3
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java View File

@@ -105,12 +105,12 @@ public abstract class Screen extends RelativeLayout implements Subscriber {
105 105
     }
106 106
 
107 107
     private void createAndAddTopBar() {
108
-        createTopBar();
108
+        topBar = createTopBar();
109 109
         addTopBar();
110 110
     }
111 111
 
112
-    protected void createTopBar() {
113
-        topBar = new TopBar(getContext());
112
+    protected TopBar createTopBar() {
113
+        return new TopBar(getContext());
114 114
     }
115 115
 
116 116
     private void addTopBar() {

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

@@ -117,6 +117,9 @@ public class CollapseCalculator {
117 117
     }
118 118
 
119 119
     private boolean canCollapse(Direction direction) {
120
+        if (view == null) {
121
+            return false;
122
+        }
120 123
         checkCollapseLimits();
121 124
         return (isNotCollapsedOrExpended() ||
122 125
                (canCollapse && isExpendedAndScrollingUp(direction)) ||

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

@@ -18,7 +18,7 @@ import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
18 18
 
19 19
 public class CollapsingTopBar extends TopBar implements CollapsingView {
20 20
     private CollapsingTopBarBackground collapsingTopBarBackground;
21
-    private CollapsingTopBarReactView header;
21
+    private CollapsingTopBarReactHeader header;
22 22
     private ScrollListener scrollListener;
23 23
     private float finalCollapsedTranslation;
24 24
     private CollapsingTopBarParams params;
@@ -71,7 +71,7 @@ public class CollapsingTopBar extends TopBar implements CollapsingView {
71 71
 
72 72
     private void createReactView(CollapsingTopBarParams params) {
73 73
         if (params.hasReactView()) {
74
-            header = new CollapsingTopBarReactView(getContext(),
74
+            header = new CollapsingTopBarReactHeader(getContext(),
75 75
                     params.reactViewId,
76 76
                     new NavigationParams(Bundle.EMPTY),
77 77
                     scrollListener);

android/app/src/main/java/com/reactnativenavigation/views/collapsingToolbar/CollapsingTopBarReactView.java → android/app/src/main/java/com/reactnativenavigation/views/collapsingToolbar/CollapsingTopBarReactHeader.java View File

@@ -13,14 +13,14 @@ import com.reactnativenavigation.NavigationApplication;
13 13
 import com.reactnativenavigation.params.NavigationParams;
14 14
 import com.reactnativenavigation.views.ContentView;
15 15
 
16
-public class CollapsingTopBarReactView extends ContentView {
16
+public class CollapsingTopBarReactHeader extends ContentView {
17 17
     private ScrollListener listener;
18 18
     private boolean mIsScrolling;
19 19
     private int mTouchSlop;
20 20
     private int touchDown = -1;
21 21
     private final JSTouchDispatcher mJSTouchDispatcher = new JSTouchDispatcher(this);
22 22
 
23
-    public CollapsingTopBarReactView(Context context, String screenId, NavigationParams navigationParams, ScrollListener scrollListener) {
23
+    public CollapsingTopBarReactHeader(Context context, String screenId, NavigationParams navigationParams, ScrollListener scrollListener) {
24 24
         super(context, screenId, navigationParams);
25 25
         listener = scrollListener;
26 26
         ViewConfiguration vc = ViewConfiguration.get(context);