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

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

14
 import com.reactnativenavigation.views.CollapsingContentView;
14
 import com.reactnativenavigation.views.CollapsingContentView;
15
 import com.reactnativenavigation.views.ContentView;
15
 import com.reactnativenavigation.views.ContentView;
16
 import com.reactnativenavigation.views.LeftButtonOnClickListener;
16
 import com.reactnativenavigation.views.LeftButtonOnClickListener;
17
+import com.reactnativenavigation.views.TopBar;
17
 import com.reactnativenavigation.views.collapsingToolbar.CollapseAmount;
18
 import com.reactnativenavigation.views.collapsingToolbar.CollapseAmount;
18
 import com.reactnativenavigation.views.collapsingToolbar.CollapseCalculator;
19
 import com.reactnativenavigation.views.collapsingToolbar.CollapseCalculator;
19
 import com.reactnativenavigation.views.collapsingToolbar.CollapsingTopBar;
20
 import com.reactnativenavigation.views.collapsingToolbar.CollapsingTopBar;
33
     }
34
     }
34
 
35
 
35
     @Override
36
     @Override
36
-    protected void createTopBar() {
37
+    protected TopBar createTopBar() {
37
         final CollapsingTopBar topBar = new CollapsingTopBar(getContext(), styleParams.collapsingTopBarParams);
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
     @Override
43
     @Override
49
     }
50
     }
50
 
51
 
51
     protected ContentView createContentView(PageParams tab) {
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
     private void setupCollapseDetection(CollapsingContentView contentView) {
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
             @Override
62
             @Override
65
             public void onScrollViewAdded(ScrollView scrollView) {
63
             public void onScrollViewAdded(ScrollView scrollView) {
66
                 ((CollapsingTopBar) topBar).onScrollViewAdded(scrollView);
64
                 ((CollapsingTopBar) topBar).onScrollViewAdded(scrollView);
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
                 new OnScrollListener() {
71
                 new OnScrollListener() {
74
                     @Override
72
                     @Override
75
                     public void onScroll(CollapseAmount amount) {
73
                     public void onScroll(CollapseAmount amount) {
76
-                        ((CollapsingView) topBar).collapse(amount);
74
+                        topBar.collapse(amount);
77
                         ((CollapsingView) viewPager).collapse(amount);
75
                         ((CollapsingView) viewPager).collapse(amount);
78
                     }
76
                     }
79
 
77
 
80
                     @Override
78
                     @Override
81
                     public void onFling(CollapseAmount amount) {
79
                     public void onFling(CollapseAmount amount) {
82
-                        ((CollapsingView) topBar).collapse(amount);
80
+                        topBar.collapse(amount);
83
                         ((CollapsingView) viewPager).collapse(amount);
81
                         ((CollapsingView) viewPager).collapse(amount);
84
                     }
82
                     }
85
                 },
83
                 },
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
     }
105
     }
106
 
106
 
107
     private void createAndAddTopBar() {
107
     private void createAndAddTopBar() {
108
-        createTopBar();
108
+        topBar = createTopBar();
109
         addTopBar();
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
     private void addTopBar() {
116
     private void addTopBar() {

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

117
     }
117
     }
118
 
118
 
119
     private boolean canCollapse(Direction direction) {
119
     private boolean canCollapse(Direction direction) {
120
+        if (view == null) {
121
+            return false;
122
+        }
120
         checkCollapseLimits();
123
         checkCollapseLimits();
121
         return (isNotCollapsedOrExpended() ||
124
         return (isNotCollapsedOrExpended() ||
122
                (canCollapse && isExpendedAndScrollingUp(direction)) ||
125
                (canCollapse && isExpendedAndScrollingUp(direction)) ||

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

18
 
18
 
19
 public class CollapsingTopBar extends TopBar implements CollapsingView {
19
 public class CollapsingTopBar extends TopBar implements CollapsingView {
20
     private CollapsingTopBarBackground collapsingTopBarBackground;
20
     private CollapsingTopBarBackground collapsingTopBarBackground;
21
-    private CollapsingTopBarReactView header;
21
+    private CollapsingTopBarReactHeader header;
22
     private ScrollListener scrollListener;
22
     private ScrollListener scrollListener;
23
     private float finalCollapsedTranslation;
23
     private float finalCollapsedTranslation;
24
     private CollapsingTopBarParams params;
24
     private CollapsingTopBarParams params;
71
 
71
 
72
     private void createReactView(CollapsingTopBarParams params) {
72
     private void createReactView(CollapsingTopBarParams params) {
73
         if (params.hasReactView()) {
73
         if (params.hasReactView()) {
74
-            header = new CollapsingTopBarReactView(getContext(),
74
+            header = new CollapsingTopBarReactHeader(getContext(),
75
                     params.reactViewId,
75
                     params.reactViewId,
76
                     new NavigationParams(Bundle.EMPTY),
76
                     new NavigationParams(Bundle.EMPTY),
77
                     scrollListener);
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
 import com.reactnativenavigation.params.NavigationParams;
13
 import com.reactnativenavigation.params.NavigationParams;
14
 import com.reactnativenavigation.views.ContentView;
14
 import com.reactnativenavigation.views.ContentView;
15
 
15
 
16
-public class CollapsingTopBarReactView extends ContentView {
16
+public class CollapsingTopBarReactHeader extends ContentView {
17
     private ScrollListener listener;
17
     private ScrollListener listener;
18
     private boolean mIsScrolling;
18
     private boolean mIsScrolling;
19
     private int mTouchSlop;
19
     private int mTouchSlop;
20
     private int touchDown = -1;
20
     private int touchDown = -1;
21
     private final JSTouchDispatcher mJSTouchDispatcher = new JSTouchDispatcher(this);
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
         super(context, screenId, navigationParams);
24
         super(context, screenId, navigationParams);
25
         listener = scrollListener;
25
         listener = scrollListener;
26
         ViewConfiguration vc = ViewConfiguration.get(context);
26
         ViewConfiguration vc = ViewConfiguration.get(context);