Browse Source

Setup TopTabs with ViewPager (#2373)

Slightly changes topTabs node declaration and removed the container from it.
Guy Carmeli 6 years ago
parent
commit
cfade8c2bb
No account linked to committer's email address

+ 2
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/NavigationOptions.java View File

@@ -31,6 +31,7 @@ public class NavigationOptions implements DEFAULT_VALUES {
31 31
 
32 32
 		result.topBarOptions = TopBarOptions.parse(json.optJSONObject("topBar"));
33 33
 		result.topTabsOptions = TopTabsOptions.parse(json.optJSONObject("topTabs"));
34
+        result.topTabOptions = TopTabOptions.parse(json.optJSONObject("topTab"));
34 35
 		result.bottomTabsOptions = BottomTabsOptions.parse(json.optJSONObject("bottomTabs"));
35 36
 
36 37
 		return result.withDefaultOptions(defaultOptions);
@@ -38,6 +39,7 @@ public class NavigationOptions implements DEFAULT_VALUES {
38 39
 
39 40
 	public TopBarOptions topBarOptions = new TopBarOptions();
40 41
     public TopTabsOptions topTabsOptions = new TopTabsOptions();
42
+    public TopTabOptions topTabOptions = new TopTabOptions();
41 43
     public BottomTabsOptions bottomTabsOptions = new BottomTabsOptions();
42 44
 
43 45
 	public void mergeWith(final NavigationOptions other) {

+ 23
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/TopTabOptions.java View File

@@ -0,0 +1,23 @@
1
+package com.reactnativenavigation.parse;
2
+
3
+import org.json.JSONObject;
4
+
5
+public class TopTabOptions implements DEFAULT_VALUES {
6
+    public String title = NO_VALUE;
7
+
8
+    public static TopTabOptions parse(JSONObject json) {
9
+        TopTabOptions result = new TopTabOptions();
10
+        if (json == null) return result;
11
+
12
+        result.title = json.optString("title", NO_VALUE);
13
+        return result;
14
+    }
15
+
16
+    void mergeWith(TopTabOptions topTabsOptions) {
17
+
18
+    }
19
+
20
+    void mergeWithDefault(TopTabOptions topTabsOptions) {
21
+
22
+    }
23
+}

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/toptabs/TopTabController.java View File

@@ -69,4 +69,8 @@ public class TopTabController extends ViewController implements NavigationOption
69 69
     public void mergeNavigationOptions(NavigationOptions options) {
70 70
         this.options.mergeWith(options);
71 71
     }
72
+
73
+    String getTabTitle() {
74
+        return options.topTabOptions.title;
75
+    }
72 76
 }

+ 5
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/toptabs/TopTabsAdapter.java View File

@@ -15,6 +15,11 @@ public class TopTabsAdapter extends PagerAdapter implements ViewPager.OnPageChan
15 15
         this.tabs = tabs;
16 16
     }
17 17
 
18
+    @Override
19
+    public CharSequence getPageTitle(int position) {
20
+        return tabs.get(position).getTabTitle();
21
+    }
22
+
18 23
     @Override
19 24
     public int getCount() {
20 25
         return tabs.size();

+ 18
- 7
lib/android/app/src/main/java/com/reactnativenavigation/views/TopBar.java View File

@@ -10,18 +10,19 @@ import android.view.View;
10 10
 import android.view.ViewGroup;
11 11
 import android.widget.TextView;
12 12
 
13
+import com.reactnativenavigation.viewcontrollers.toptabs.TopTabsViewPager;
14
+
13 15
 public class TopBar extends AppBarLayout {
14 16
 	private final Toolbar titleBar;
15
-	private final TopTabs topTabs;
17
+	private TopTabs topTabs;
16 18
 
17
-	public TopBar(final Context context) {
18
-		super(context);
19
-		titleBar = new Toolbar(context);
20
-        topTabs = new TopTabs(context);
19
+    public TopBar(final Context context) {
20
+        super(context);
21
+        titleBar = new Toolbar(context);
21 22
         addView(titleBar);
22
-	}
23
+    }
23 24
 
24
-	public void setTitle(String title) {
25
+    public void setTitle(String title) {
25 26
 		titleBar.setTitle(title);
26 27
 	}
27 28
 
@@ -73,4 +74,14 @@ public class TopBar extends AppBarLayout {
73 74
 	public Toolbar getTitleBar() {
74 75
 		return titleBar;
75 76
 	}
77
+
78
+    public void setupTopTabsWithViewPager(TopTabsViewPager viewPager) {
79
+        initTopTabs();
80
+        topTabs.setupWithViewPager(viewPager);
81
+    }
82
+
83
+    private void initTopTabs() {
84
+        topTabs = new TopTabs(getContext());
85
+        addView(topTabs);
86
+    }
76 87
 }

+ 1
- 0
lib/android/app/src/main/java/com/reactnativenavigation/views/TopTabsLayout.java View File

@@ -35,6 +35,7 @@ public class TopTabsLayout extends LinearLayout implements Container {
35 35
         setOrientation(VERTICAL);
36 36
         addView(topBar);
37 37
         addView(viewPager);
38
+        topBar.setupTopTabsWithViewPager(viewPager);
38 39
     }
39 40
 
40 41
     @Override

+ 1
- 1
lib/src/commands/LayoutTreeParser.js View File

@@ -39,7 +39,7 @@ class LayoutTreeParser {
39 39
   _createTopTabs(topTabs) {
40 40
     return {
41 41
       type: LayoutTypes.TopTabs,
42
-      children: _.map(topTabs, (t) => this._createTopTab(t.container))
42
+      children: _.map(topTabs, (t) => this._createTopTab(t))
43 43
     };
44 44
   }
45 45
 

+ 3
- 9
lib/src/commands/SimpleLayouts.js View File

@@ -114,19 +114,13 @@ const tabBasedWithBothSideMenus = {
114 114
 const singleScreenWithTopTabs = {
115 115
   topTabs: [
116 116
     {
117
-      container: {
118
-        name: 'navigation.playground.TextScreen'
119
-      }
117
+      name: 'navigation.playground.TextScreen'
120 118
     },
121 119
     {
122
-      container: {
123
-        name: 'navigation.playground.TextScreen'
124
-      }
120
+      name: 'navigation.playground.TextScreen'
125 121
     },
126 122
     {
127
-      container: {
128
-        name: 'navigation.playground.TextScreen'
129
-      }
123
+      name: 'navigation.playground.TextScreen'
130 124
     }
131 125
   ]
132 126
 };

+ 24
- 15
playground/src/containers/WelcomeScreen.js View File

@@ -151,29 +151,38 @@ class WelcomeScreen extends Component {
151 151
     Navigation.push(this.props.containerId, {
152 152
       topTabs: [
153 153
         {
154
-          container: {
155
-            name: 'navigation.playground.TopTabOptionsScreen',
156
-            passProps: {
157
-              title: 'Tab 1',
158
-              text: 'This is top tab 1'
154
+          name: 'navigation.playground.TopTabOptionsScreen',
155
+          passProps: {
156
+            title: 'Tab 1',
157
+            text: 'This is top tab 1'
158
+          },
159
+          navigationOptions: {
160
+            topTab: {
161
+              title: 'Tab 1'
159 162
             }
160 163
           }
161 164
         },
162 165
         {
163
-          container: {
164
-            name: 'navigation.playground.TopTabScreen',
165
-            passProps: {
166
-              title: 'Tab 2',
167
-              text: 'This is top tab 2'
166
+          name: 'navigation.playground.TopTabScreen',
167
+          passProps: {
168
+            title: 'Tab 2',
169
+            text: 'This is top tab 2'
170
+          },
171
+          navigationOptions: {
172
+            topTab: {
173
+              title: 'Tab 2'
168 174
             }
169 175
           }
170 176
         },
171 177
         {
172
-          container: {
173
-            name: 'navigation.playground.TopTabScreen',
174
-            passProps: {
175
-              title: 'Tab 3',
176
-              text: 'This is top tab 3'
178
+          name: 'navigation.playground.TopTabScreen',
179
+          passProps: {
180
+            title: 'Tab 3',
181
+            text: 'This is top tab 3'
182
+          },
183
+          navigationOptions: {
184
+            topTab: {
185
+              title: 'Tab 3'
177 186
             }
178 187
           }
179 188
         }