浏览代码

Add selectedTabChanged event

Guy Carmeli 8 年前
父节点
当前提交
a177b65d0c

+ 7
- 0
android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java 查看文件

@@ -89,6 +89,13 @@ public abstract class NavigationApplication extends Application {
89 89
         navigationReactInstance.getReactEventEmitter().sendNavigatorEvent(eventId, navigatorEventId);
90 90
     }
91 91
 
92
+    public void sendNavigatorEvent(String eventId, String navigatorEventId, WritableMap data) {
93
+        if (navigationReactInstance == null) {
94
+            return;
95
+        }
96
+        navigationReactInstance.getReactEventEmitter().sendNavigatorEvent(eventId, navigatorEventId, data);
97
+    }
98
+
92 99
     public void sendEvent(String eventId, String navigatorEventId) {
93 100
         if (navigationReactInstance == null) {
94 101
             return;

+ 6
- 0
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactEventEmitter.java 查看文件

@@ -25,6 +25,12 @@ public class NavigationReactEventEmitter {
25 25
         eventEmitter.emit(navigatorEventId, data);
26 26
     }
27 27
 
28
+    public void sendNavigatorEvent(String eventId, String navigatorEventId, WritableMap data) {
29
+        data.putString(KEY_NAVIGATOR_EVENT_ID, navigatorEventId);
30
+        data.putString(KEY_EVENT_ID, eventId);
31
+        eventEmitter.emit(navigatorEventId, data);
32
+    }
33
+
28 34
     public void sendEvent(String eventId, String data) {
29 35
         eventEmitter.emit(eventId, data);
30 36
     }

+ 0
- 1
android/app/src/main/java/com/reactnativenavigation/params/TopTabParams.java 查看文件

@@ -5,6 +5,5 @@ import android.os.Bundle;
5 5
 public class TopTabParams {
6 6
     public String screenId;
7 7
     public String title;
8
-    public String screenInstanceId;
9 8
     public Bundle passProps;
10 9
 }

+ 0
- 2
android/app/src/main/java/com/reactnativenavigation/params/parsers/TopTabParamsParser.java 查看文件

@@ -10,7 +10,6 @@ import java.util.List;
10 10
 
11 11
 public class TopTabParamsParser extends Parser {
12 12
     private static final String KEY_SCREEN_ID = "screenId";
13
-    private static final String KEY_SCREEN_INSTANCE_ID = "screenInstanceID";
14 13
     private static final String KEY_TITLE = "title";
15 14
     private static final String KEY_PROPS = "passProps";
16 15
 
@@ -27,7 +26,6 @@ public class TopTabParamsParser extends Parser {
27 26
     private static TopTabParams parseItem(Bundle params) {
28 27
         TopTabParams result = new TopTabParams();
29 28
         result.screenId = params.getString(KEY_SCREEN_ID);
30
-        result.screenInstanceId = params.getString(KEY_SCREEN_INSTANCE_ID);
31 29
         result.title = params.getString(KEY_TITLE);
32 30
         result.passProps = params.getBundle(KEY_PROPS);
33 31
         return result;

+ 61
- 0
android/app/src/main/java/com/reactnativenavigation/screens/ContentViewPagerAdapter.java 查看文件

@@ -0,0 +1,61 @@
1
+package com.reactnativenavigation.screens;
2
+
3
+import android.support.v4.view.PagerAdapter;
4
+import android.view.View;
5
+import android.view.ViewGroup;
6
+
7
+import com.facebook.react.bridge.Arguments;
8
+import com.facebook.react.bridge.WritableMap;
9
+import com.reactnativenavigation.NavigationApplication;
10
+import com.reactnativenavigation.params.TopTabParams;
11
+import com.reactnativenavigation.views.ContentView;
12
+
13
+import java.util.List;
14
+
15
+public class ContentViewPagerAdapter extends PagerAdapter {
16
+
17
+    private List<ContentView> contentViews;
18
+    private List<TopTabParams> topTabParams;
19
+    private int currentPosition = 0;
20
+
21
+    public ContentViewPagerAdapter(List<ContentView> contentViews, List<TopTabParams> topTabParams) {
22
+        this.contentViews = contentViews;
23
+        this.topTabParams = topTabParams;
24
+    }
25
+
26
+    @Override
27
+    public Object instantiateItem(ViewGroup container, int position) {
28
+        return contentViews.get(position);
29
+    }
30
+
31
+    @Override
32
+    public void setPrimaryItem(ViewGroup container, int position, Object object) {
33
+        super.setPrimaryItem(container, position, object);
34
+        if (position != currentPosition) {
35
+            currentPosition = position;
36
+            sendPageChangeEvent();
37
+        }
38
+    }
39
+
40
+    private void sendPageChangeEvent() {
41
+        WritableMap data = Arguments.createMap();
42
+        String navigatorEventId = contentViews.get(currentPosition).getNavigatorEventId();
43
+        data.putInt("position", currentPosition);
44
+        NavigationApplication.instance.sendNavigatorEvent("selectedTabChanged", navigatorEventId, data);
45
+    }
46
+
47
+    @Override
48
+    public int getCount() {
49
+        return contentViews.size();
50
+    }
51
+
52
+    @Override
53
+    public boolean isViewFromObject(View view, Object object) {
54
+        return view == object;
55
+    }
56
+
57
+    @Override
58
+    public CharSequence getPageTitle(int position) {
59
+        return topTabParams.get(position).title;
60
+    }
61
+}

+ 1
- 4
android/app/src/main/java/com/reactnativenavigation/screens/FragmentScreen.java 查看文件

@@ -38,10 +38,7 @@ public class FragmentScreen extends Screen {
38 38
     }
39 39
 
40 40
     private void addContent() {
41
-        ContentView contentView = new ContentView(getContext(),
42
-                screenParams.screenId,
43
-                screenParams.passProps,
44
-                screenParams.navigationParams);
41
+        ContentView contentView = new ContentView(getContext(), screenParams, screenParams.screenId);
45 42
         addView(contentView, 0, 0);
46 43
         LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
47 44
         if (screenParams.styleParams.drawScreenBelowTopBar) {

+ 1
- 4
android/app/src/main/java/com/reactnativenavigation/screens/SingleScreen.java 查看文件

@@ -19,10 +19,7 @@ public class SingleScreen extends Screen {
19 19
 
20 20
     @Override
21 21
     protected void createContent() {
22
-        contentView = new ContentView(getContext(),
23
-                screenParams.screenId,
24
-                screenParams.passProps,
25
-                screenParams.navigationParams);
22
+        contentView = new ContentView(getContext(), screenParams, screenParams.screenId);
26 23
         LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
27 24
         if (screenParams.styleParams.drawScreenBelowTopBar) {
28 25
             params.addRule(BELOW, topBar.getId());

+ 4
- 79
android/app/src/main/java/com/reactnativenavigation/screens/ViewPagerScreen.java 查看文件

@@ -1,11 +1,8 @@
1 1
 package com.reactnativenavigation.screens;
2 2
 
3 3
 import android.support.design.widget.TabLayout;
4
-import android.support.v4.view.PagerAdapter;
5 4
 import android.support.v4.view.ViewPager;
6 5
 import android.support.v7.app.AppCompatActivity;
7
-import android.view.View;
8
-import android.view.ViewGroup;
9 6
 
10 7
 import com.reactnativenavigation.params.ScreenParams;
11 8
 import com.reactnativenavigation.params.TopTabParams;
@@ -19,6 +16,7 @@ import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
19 16
 
20 17
 public class ViewPagerScreen extends Screen {
21 18
 
19
+    private static final int OFFSCREEN_PAGE_LIMIT = 99;
22 20
     private List<ContentView> contentViews;
23 21
     private ViewPager viewPager;
24 22
 
@@ -36,7 +34,7 @@ public class ViewPagerScreen extends Screen {
36 34
 
37 35
     private void createViewPager() {
38 36
         viewPager = new ViewPager(getContext());
39
-        viewPager.setOffscreenPageLimit(99);
37
+        viewPager.setOffscreenPageLimit(OFFSCREEN_PAGE_LIMIT);
40 38
         LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
41 39
         if (screenParams.styleParams.drawScreenBelowTopBar) {
42 40
             lp.addRule(BELOW, topBar.getId());
@@ -47,18 +45,14 @@ public class ViewPagerScreen extends Screen {
47 45
     private void addPages() {
48 46
         contentViews = new ArrayList<>();
49 47
         for (TopTabParams topTabParam : screenParams.topTabParams) {
50
-            ContentView contentView = new ContentView(getContext(),
51
-                    topTabParam.screenId,
52
-                    screenParams.passProps,
53
-                    screenParams.navigationParams);
48
+            ContentView contentView = new ContentView(getContext(), screenParams, topTabParam.screenId);
54 49
             addContent(contentView);
55 50
             contentViews.add(contentView);
56 51
         }
57 52
     }
58 53
 
59 54
     private void setupViewPager(TabLayout tabLayout) {
60
-        ContentViewPagerAdapter adapter =
61
-                new ContentViewPagerAdapter(viewPager, contentViews, screenParams.topTabParams);
55
+        ContentViewPagerAdapter adapter = new ContentViewPagerAdapter(contentViews, screenParams.topTabParams);
62 56
         viewPager.setAdapter(adapter);
63 57
         tabLayout.setupWithViewPager(viewPager);
64 58
     }
@@ -88,73 +82,4 @@ public class ViewPagerScreen extends Screen {
88 82
             contentView.preventMountAfterReattachedToWindow();
89 83
         }
90 84
     }
91
-
92
-    public class ContentViewPagerAdapter extends PagerAdapter implements TabLayout.OnTabSelectedListener, ViewPager.OnPageChangeListener {
93
-
94
-        private ViewPager viewPager;
95
-        private List<ContentView> contentViews;
96
-        private List<TopTabParams> topTabParams;
97
-
98
-        public ContentViewPagerAdapter(ViewPager viewPager, List<ContentView> contentViews, List<TopTabParams> topTabParams) {
99
-            this.viewPager = viewPager;
100
-            this.contentViews = contentViews;
101
-            this.topTabParams = topTabParams;
102
-        }
103
-
104
-        @Override
105
-        public Object instantiateItem(ViewGroup container, int position) {
106
-            return contentViews.get(position);
107
-        }
108
-
109
-        @Override
110
-        public void destroyItem(ViewGroup container, int position, Object view) {
111
-
112
-        }
113
-
114
-        @Override
115
-        public int getCount() {
116
-            return contentViews.size();
117
-        }
118
-
119
-        @Override
120
-        public boolean isViewFromObject(View view, Object object) {
121
-            return view == object;
122
-        }
123
-
124
-        @Override
125
-        public CharSequence getPageTitle(int position) {
126
-            return topTabParams.get(position).title;
127
-        }
128
-
129
-        @Override
130
-        public void onTabSelected(TabLayout.Tab tab) {
131
-            int position = tab.getPosition();
132
-            viewPager.setCurrentItem(position);
133
-        }
134
-
135
-        @Override
136
-        public void onTabUnselected(TabLayout.Tab tab) {
137
-
138
-        }
139
-
140
-        @Override
141
-        public void onTabReselected(TabLayout.Tab tab) {
142
-
143
-        }
144
-
145
-        @Override
146
-        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
147
-
148
-        }
149
-
150
-        @Override
151
-        public void onPageSelected(int position) {
152
-
153
-        }
154
-
155
-        @Override
156
-        public void onPageScrollStateChanged(int state) {
157
-
158
-        }
159
-    }
160 85
 }

+ 21
- 13
android/app/src/main/java/com/reactnativenavigation/views/ContentView.java 查看文件

@@ -6,32 +6,28 @@ import android.os.Bundle;
6 6
 import com.facebook.react.ReactInstanceManager;
7 7
 import com.facebook.react.ReactRootView;
8 8
 import com.reactnativenavigation.NavigationApplication;
9
+import com.reactnativenavigation.params.ScreenParams;
9 10
 import com.reactnativenavigation.react.ReactViewHacks;
10 11
 
11 12
 public class ContentView extends ReactRootView {
12 13
 
13 14
     private final String screenId;
15
+    private final String navigatorEventId;
14 16
     private final Bundle passProps;
15 17
     private Bundle navigationParams;
16 18
 
17
-    public ContentView(Context context, final String screenId, Bundle passProps, Bundle navigationParams) {
19
+    public ContentView(Context context, ScreenParams screenParams, String screenId) {
18 20
         super(context);
19 21
         this.screenId = screenId;
20
-        this.passProps = passProps;
21
-        this.navigationParams = navigationParams;
22
-        attachToJS();
23
-    }
22
+        navigatorEventId = screenParams.navigatorEventId;
24 23
 
25
-    private void attachToJS() {
26
-        ReactInstanceManager react = NavigationApplication.instance.getNavigationReactInstance().getReactInstanceManager();
27
-        startReactApplication(react, screenId, mergePropsAndNavigationParams());
24
+        passProps = screenParams.passProps;
25
+        navigationParams = screenParams.navigationParams;
26
+        attachToJS();
28 27
     }
29 28
 
30
-    private Bundle mergePropsAndNavigationParams() {
31
-        if (passProps != null) {
32
-            navigationParams.putAll(passProps);
33
-        }
34
-        return navigationParams;
29
+    public String getNavigatorEventId() {
30
+        return navigatorEventId;
35 31
     }
36 32
 
37 33
     public void preventUnmountOnDetachedFromWindow() {
@@ -45,4 +41,16 @@ public class ContentView extends ReactRootView {
45 41
     public void preventMountAfterReattachedToWindow() {
46 42
         ReactViewHacks.preventMountAfterReattachedToWindow(this);
47 43
     }
44
+
45
+    private void attachToJS() {
46
+        ReactInstanceManager react = NavigationApplication.instance.getNavigationReactInstance().getReactInstanceManager();
47
+        startReactApplication(react, screenId, mergePropsAndNavigationParams());
48
+    }
49
+
50
+    private Bundle mergePropsAndNavigationParams() {
51
+        if (passProps != null) {
52
+            navigationParams.putAll(passProps);
53
+        }
54
+        return navigationParams;
55
+    }
48 56
 }