Browse Source

Toggle tabs

Guy Carmeli 8 years ago
parent
commit
337ab33d5e

+ 8
- 17
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java View File

5
 import android.os.AsyncTask;
5
 import android.os.AsyncTask;
6
 import android.os.Bundle;
6
 import android.os.Bundle;
7
 import android.support.design.widget.CoordinatorLayout;
7
 import android.support.design.widget.CoordinatorLayout;
8
-import android.view.View;
9
 
8
 
10
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
9
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
11
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
10
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
115
         StyleHelper.updateStyles(mToolbar, getCurrentScreen());
114
         StyleHelper.updateStyles(mToolbar, getCurrentScreen());
116
 
115
 
117
         if (shouldToggleTabs(screen)) {
116
         if (shouldToggleTabs(screen)) {
118
-            toggleTabs(screen.bottomTabsHidden, false);
117
+            mBottomNavigation.toggleTabs(screen.bottomTabsHidden, false);
119
         }
118
         }
120
     }
119
     }
121
 
120
 
129
                 StyleHelper.updateStyles(mToolbar, currentScreen);
128
                 StyleHelper.updateStyles(mToolbar, currentScreen);
130
 
129
 
131
                 if (shouldToggleTabs(currentScreen)) {
130
                 if (shouldToggleTabs(currentScreen)) {
132
-                    toggleTabs(currentScreen.bottomTabsHidden, false);
131
+                    mBottomNavigation.toggleTabs(currentScreen.bottomTabsHidden, false);
133
                 }
132
                 }
134
 
133
 
135
                 return popped;
134
                 return popped;
148
                 StyleHelper.updateStyles(mToolbar, currentScreen);
147
                 StyleHelper.updateStyles(mToolbar, currentScreen);
149
 
148
 
150
                 if (shouldToggleTabs(currentScreen)) {
149
                 if (shouldToggleTabs(currentScreen)) {
151
-                    toggleTabs(currentScreen.bottomTabsHidden, false);
150
+                    mBottomNavigation.toggleTabs(currentScreen.bottomTabsHidden, false);
152
                 }
151
                 }
153
 
152
 
154
                 return popped;
153
                 return popped;
236
     public void toggleTabs(ReadableMap params) {
235
     public void toggleTabs(ReadableMap params) {
237
         boolean hide = params.getBoolean(KEY_HIDDEN);
236
         boolean hide = params.getBoolean(KEY_HIDDEN);
238
         boolean animated = params.getBoolean(KEY_ANIMATED);
237
         boolean animated = params.getBoolean(KEY_ANIMATED);
239
-        toggleTabs(hide, animated);
240
-    }
241
-
242
-    // TODO: support animated = false -guyca
243
-    private void toggleTabs(boolean hide, boolean animated) {
244
-        if (hide) {
245
-//            mBottomNavigation.hideBottomNavigation(animated);
246
-            mBottomNavigation.setVisibility(View.GONE);
247
-        } else {
248
-            mBottomNavigation.setVisibility(View.VISIBLE);
249
-//            mBottomNavigation.restoreBottomNavigation(animated);
250
-        }
238
+        mBottomNavigation.toggleTabs(hide, animated);
251
     }
239
     }
252
 
240
 
253
     private boolean shouldToggleTabs(Screen newScreen) {
241
     private boolean shouldToggleTabs(Screen newScreen) {
254
         return mBottomNavigation.isShown() == newScreen.bottomTabsHidden;
242
         return mBottomNavigation.isShown() == newScreen.bottomTabsHidden;
255
     }
243
     }
256
 
244
 
245
+    public void notifyScroll(int direction) {
246
+        mBottomNavigation.onScroll(direction);
247
+    }
248
+
257
     private static class SetupTabsTask extends AsyncTask<Void, Void, Map<Screen, Drawable>> {
249
     private static class SetupTabsTask extends AsyncTask<Void, Void, Map<Screen, Drawable>> {
258
         private BottomTabActivity mActivity;
250
         private BottomTabActivity mActivity;
259
         private RnnToolBar mToolBar;
251
         private RnnToolBar mToolBar;
308
         this.onTabSelected(0, false);
300
         this.onTabSelected(0, false);
309
     }
301
     }
310
 
302
 
311
-
312
     @Override
303
     @Override
313
     protected void removeAllReactViews() {
304
     protected void removeAllReactViews() {
314
         for (ScreenStack screenStack : mScreenStacks) {
305
         for (ScreenStack screenStack : mScreenStacks) {

+ 26
- 4
android/app/src/main/java/com/reactnativenavigation/views/BottomNavigation.java View File

2
 
2
 
3
 import android.content.Context;
3
 import android.content.Context;
4
 import android.util.AttributeSet;
4
 import android.util.AttributeSet;
5
+import android.view.View;
5
 
6
 
6
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
7
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
7
 
8
 
10
  */
11
  */
11
 public class BottomNavigation extends AHBottomNavigation {
12
 public class BottomNavigation extends AHBottomNavigation {
12
 
13
 
14
+    public static final int SCROLL_DIRECTION_UP = 0;
15
+    public static final int SCROLL_DIRECTION_DOWN = 1;
16
+
13
     public BottomNavigation(Context context) {
17
     public BottomNavigation(Context context) {
14
         super(context);
18
         super(context);
15
     }
19
     }
22
         super(context, attrs, defStyleAttr);
26
         super(context, attrs, defStyleAttr);
23
     }
27
     }
24
 
28
 
25
-    /**
26
-     * Synchronize the BottomNavigation with a {@link android.widget.ScrollView}. When
27
-     */
28
-    public void setupWithScrollView() {
29
+    // TODO: support animated = false -guyca
30
+    public void toggleTabs(boolean hide, boolean animated) {
31
+        if (hide) {
32
+            hide();
33
+        } else {
34
+            show();
35
+        }
36
+    }
29
 
37
 
38
+    private void hide() {
39
+        // mBottomNavigation.hideBottomNavigation(animated);
40
+        setVisibility(View.GONE);
30
     }
41
     }
31
 
42
 
43
+    private void show() {
44
+        setVisibility(View.VISIBLE);
45
+        // mBottomNavigation.restoreBottomNavigation(animated);
46
+    }
32
 
47
 
48
+    public void onScroll(int direction) {
49
+        if (direction == SCROLL_DIRECTION_DOWN) {
50
+            hideBottomNavigation(true);
51
+        } else if (direction == SCROLL_DIRECTION_UP) {
52
+            restoreBottomNavigation(true);
53
+        }
54
+    }
33
 }
55
 }

+ 14
- 1
android/app/src/main/java/com/reactnativenavigation/views/RctView.java View File

11
 import com.facebook.react.ReactInstanceManager;
11
 import com.facebook.react.ReactInstanceManager;
12
 import com.facebook.react.ReactRootView;
12
 import com.facebook.react.ReactRootView;
13
 import com.reactnativenavigation.activities.BaseReactActivity;
13
 import com.reactnativenavigation.activities.BaseReactActivity;
14
+import com.reactnativenavigation.activities.BottomTabActivity;
14
 import com.reactnativenavigation.core.objects.Screen;
15
 import com.reactnativenavigation.core.objects.Screen;
15
 import com.reactnativenavigation.utils.BridgeUtils;
16
 import com.reactnativenavigation.utils.BridgeUtils;
16
 import com.reactnativenavigation.utils.ReflectionUtils;
17
 import com.reactnativenavigation.utils.ReflectionUtils;
20
  */
21
  */
21
 public class RctView extends FrameLayout {
22
 public class RctView extends FrameLayout {
22
     private static final String TAG = "RctView";
23
     private static final String TAG = "RctView";
24
+
25
+    private BottomTabActivity mContext;
23
     private ReactRootView mReactRootView;
26
     private ReactRootView mReactRootView;
24
     private ScrollView mScrollView;
27
     private ScrollView mScrollView;
28
+    private int mLastScrollY = -1;
25
     private final ViewTreeObserver.OnScrollChangedListener mScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() {
29
     private final ViewTreeObserver.OnScrollChangedListener mScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() {
26
         @Override
30
         @Override
27
         public void onScrollChanged() {
31
         public void onScrollChanged() {
28
             if (mScrollView.getViewTreeObserver().isAlive()) {
32
             if (mScrollView.getViewTreeObserver().isAlive()) {
29
-                Log.i(TAG, "onScrollChanged: " + mScrollView.getScrollY());
33
+                if (mScrollView.getScrollY() != mLastScrollY) {
34
+                    int currentScrollY = mScrollView.getScrollY();
35
+                    mContext.notifyScroll(currentScrollY > mLastScrollY ?
36
+                            BottomNavigation.SCROLL_DIRECTION_DOWN :
37
+                            BottomNavigation.SCROLL_DIRECTION_UP);
38
+
39
+                    mLastScrollY = currentScrollY;
40
+                }
41
+                Log.i(TAG, "onScrollChanged: " + mLastScrollY);
30
             }
42
             }
31
         }
43
         }
32
     };
44
     };
119
     private void setupScrollViewWithBottomTabs() {
131
     private void setupScrollViewWithBottomTabs() {
120
         mScrollView = getScrollView((ViewGroup) getParent());
132
         mScrollView = getScrollView((ViewGroup) getParent());
121
         if (mScrollView != null) {
133
         if (mScrollView != null) {
134
+            mContext = (BottomTabActivity) getContext();
122
             attachStateChangeListener(mScrollView);
135
             attachStateChangeListener(mScrollView);
123
             addScrollListener();
136
             addScrollListener();
124
         }
137
         }