|
@@ -2,40 +2,39 @@ package com.reactnativenavigation.views;
|
2
|
2
|
|
3
|
3
|
import android.annotation.SuppressLint;
|
4
|
4
|
import android.content.Context;
|
5
|
|
-import android.support.annotation.RestrictTo;
|
6
|
5
|
import android.support.v4.view.ViewPager;
|
7
|
|
-import android.view.View;
|
8
|
6
|
import android.view.ViewGroup;
|
9
|
7
|
import android.widget.RelativeLayout;
|
10
|
8
|
|
11
|
9
|
import com.reactnativenavigation.parse.Options;
|
|
10
|
+import com.reactnativenavigation.viewcontrollers.ComponentViewController.IReactView;
|
12
|
11
|
import com.reactnativenavigation.viewcontrollers.ViewController;
|
13
|
12
|
import com.reactnativenavigation.viewcontrollers.toptabs.TopTabsAdapter;
|
14
|
|
-import com.reactnativenavigation.viewcontrollers.toptabs.TopTabsViewPager;
|
15
|
13
|
|
16
|
14
|
import java.util.List;
|
17
|
15
|
|
|
16
|
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
|
|
17
|
+import static android.widget.RelativeLayout.BELOW;
|
|
18
|
+
|
18
|
19
|
@SuppressLint("ViewConstructor")
|
19
|
|
-public class TopTabsLayout extends RelativeLayout implements Component, TitleBarButton.OnClickListener {
|
|
20
|
+public class TopTabsLayout extends ViewPager implements Component, TitleBarButton.OnClickListener {
|
20
|
21
|
|
21
|
|
- private TopBar topBar;
|
22
|
|
- private TopTabsViewPager viewPager;
|
|
22
|
+ private static final int OFFSCREEN_PAGE_LIMIT = 99;
|
|
23
|
+ private List<ViewController> tabs;
|
23
|
24
|
|
24
|
25
|
public TopTabsLayout(Context context, List<ViewController> tabs, TopTabsAdapter adapter) {
|
25
|
26
|
super(context);
|
26
|
|
- viewPager = new TopTabsViewPager(context, tabs, adapter);
|
27
|
|
- topBar = new TopBar(context, this);
|
28
|
|
- topBar.setId(View.generateViewId());
|
29
|
|
-
|
30
|
|
- initViews();
|
|
27
|
+ this.tabs = tabs;
|
|
28
|
+ initTabs(adapter);
|
31
|
29
|
}
|
32
|
30
|
|
33
|
|
- private void initViews() {
|
34
|
|
- LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
35
|
|
- layoutParams.addRule(BELOW, topBar.getId());
|
36
|
|
- addView(topBar, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
37
|
|
- addView(viewPager, layoutParams);
|
38
|
|
- topBar.setupTopTabsWithViewPager(viewPager);
|
|
31
|
+ private void initTabs(TopTabsAdapter adapter) {
|
|
32
|
+ setOffscreenPageLimit(OFFSCREEN_PAGE_LIMIT);
|
|
33
|
+ for (ViewController tab : tabs) {
|
|
34
|
+ addView(tab.getView(), new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
|
|
35
|
+ }
|
|
36
|
+ setAdapter(adapter);
|
|
37
|
+ addOnPageChangeListener(adapter);
|
39
|
38
|
}
|
40
|
39
|
|
41
|
40
|
@Override
|
|
@@ -45,29 +44,30 @@ public class TopTabsLayout extends RelativeLayout implements Component, TitleBar
|
45
|
44
|
|
46
|
45
|
@Override
|
47
|
46
|
public void drawBehindTopBar() {
|
48
|
|
-
|
|
47
|
+ RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
|
|
48
|
+ layoutParams.removeRule(BELOW);
|
|
49
|
+ setLayoutParams(layoutParams);
|
49
|
50
|
}
|
50
|
51
|
|
51
|
52
|
@Override
|
52
|
53
|
public void drawBelowTopBar(TopBar topBar) {
|
53
|
|
-
|
54
|
|
- }
|
55
|
|
-
|
56
|
|
- @RestrictTo(RestrictTo.Scope.TESTS)
|
57
|
|
- public ViewPager getViewPager() {
|
58
|
|
- return viewPager;
|
|
54
|
+ RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
|
|
55
|
+ layoutParams.addRule(BELOW, topBar.getId());
|
|
56
|
+ setLayoutParams(layoutParams);
|
59
|
57
|
}
|
60
|
58
|
|
61
|
59
|
public void switchToTab(int index) {
|
62
|
|
- viewPager.setCurrentItem(index);
|
63
|
|
- }
|
64
|
|
-
|
65
|
|
- public int getCurrentItem() {
|
66
|
|
- return viewPager.getCurrentItem();
|
|
60
|
+ setCurrentItem(index);
|
67
|
61
|
}
|
68
|
62
|
|
69
|
63
|
@Override
|
70
|
64
|
public void onPress(String buttonId) {
|
71
|
|
- viewPager.sendOnNavigationButtonPressed(buttonId);
|
|
65
|
+ ((IReactView) tabs.get(getCurrentItem()).getView()).sendOnNavigationButtonPressed(buttonId);
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ public void destroy() {
|
|
69
|
+ for (ViewController tab : tabs) {
|
|
70
|
+ tab.destroy();
|
|
71
|
+ }
|
72
|
72
|
}
|
73
|
73
|
}
|