Browse Source

ScrollViewAttacher

Daniel Zlotin 8 years ago
parent
commit
e76d9682e3

+ 0
- 17
android/app/src/main/java/com/reactnativenavigation/views/ContentView.java View File

@@ -17,24 +17,7 @@ public class ContentView extends ReactRootView {
17 17
         startReactApplication(reactInstanceManager, moduleName, passProps);
18 18
     }
19 19
 
20
-    @Nullable
21
-    public ScrollView findScrollView() {
22
-        return findScrollView(this);
23
-    }
24
-
25
-    private ScrollView findScrollView(ViewGroup parent) {
26
-        for (int i = 0; i < parent.getChildCount(); i++) {
27
-            View child = parent.getChildAt(i);
28 20
 
29
-            if (child instanceof ScrollView) {
30
-                return (ScrollView) child;
31
-            } else if (child instanceof ViewGroup) {
32
-                return findScrollView((ViewGroup) child);
33
-            }
34
-        }
35
-
36
-        return null;
37
-    }
38 21
 
39 22
     @Override
40 23
     protected void onDetachedFromWindow() {

+ 0
- 52
android/app/src/main/java/com/reactnativenavigation/views/RctView.java View File

@@ -22,36 +22,6 @@ public class RctView extends FrameLayout {
22 22
 
23 23
     private BottomTabActivity context;
24 24
     private ReactRootView reactRootView;
25
-    private ScrollView scrollView;
26
-    private boolean isScrollEventListenerRegistered = false;
27
-
28
-    private final View.OnAttachStateChangeListener stateChangeListener =
29
-            new View.OnAttachStateChangeListener() {
30
-                @Override
31
-                public void onViewAttachedToWindow(View v) {
32
-                    scrollView = getScrollView((ViewGroup) getParent());
33
-
34
-                    if (scrollView != null && !isScrollEventListenerRegistered) {
35
-                        addScrollListener();
36
-                    }
37
-                }
38
-
39
-                @Override
40
-                public void onViewDetachedFromWindow(final View detachedView) {
41
-                    removeScrollListener();
42
-
43
-                    post(new Runnable() {
44
-                        @Override
45
-                        public void run() {
46
-                            scrollView = getScrollView((ViewGroup) getParent());
47
-                            if (scrollView != null && !isScrollEventListenerRegistered) {
48
-                                isScrollEventListenerRegistered = true;
49
-                                addScrollListener();
50
-                            }
51
-                        }
52
-                    });
53
-                }
54
-            };
55 25
 
56 26
     /**
57 27
      * Interface used to run some code when the {@link ReactRootView} is visible.
@@ -101,28 +71,6 @@ public class RctView extends FrameLayout {
101 71
         return passProps;
102 72
     }
103 73
 
104
-    private void setupScrollViewWithBottomTabs() {
105
-        scrollView = getScrollView((ViewGroup) getParent());
106
-        if (scrollView != null) {
107
-            context = (BottomTabActivity) getContext();
108
-            attachStateChangeListener(scrollView);
109
-            addScrollListener();
110
-        }
111
-    }
112
-
113
-
114
-    private void attachStateChangeListener(ScrollView scrollView) {
115
-        scrollView.addOnAttachStateChangeListener(stateChangeListener);
116
-    }
117
-
118
-    private void addScrollListener() {
119
-        scrollView.getViewTreeObserver().addOnScrollChangedListener(scrollChangedListener);
120
-    }
121
-
122
-    private void removeScrollListener() {
123
-        scrollView.getViewTreeObserver().removeOnScrollChangedListener(scrollChangedListener);
124
-    }
125
-
126 74
     /**
127 75
      * Must be called before view is removed from screen, but will be added again later. Setting mAttachScheduled
128 76
      * to true will prevent the component from getting unmounted once view is detached from screen.

+ 6
- 2
android/app/src/main/java/com/reactnativenavigation/views/ScrollDirectionListener.java View File

@@ -13,7 +13,7 @@ public class ScrollDirectionListener implements ViewTreeObserver.OnScrollChanged
13 13
     }
14 14
 
15 15
     private final ViewGroup view;
16
-    private final OnChanged onChanged;
16
+    private OnChanged onChanged;
17 17
     private int lastScrollY = -1;
18 18
 
19 19
     public ScrollDirectionListener(ViewGroup view, OnChanged onChanged) {
@@ -30,10 +30,14 @@ public class ScrollDirectionListener implements ViewTreeObserver.OnScrollChanged
30 30
         final int scrollY = view.getScrollY();
31 31
         if (isScrollPositionChanged(scrollY) && !isTopOverscroll(scrollY) && !isBottomOverscroll(scrollY)) {
32 32
             lastScrollY = scrollY;
33
-            onChanged.onChanged(scrollY > lastScrollY ? Direction.Down : Direction.Up);
33
+            onChanged.onChanged(getDirection(scrollY));
34 34
         }
35 35
     }
36 36
 
37
+    private Direction getDirection(int scrollY) {
38
+        return scrollY > lastScrollY ? Direction.Down : Direction.Up;
39
+    }
40
+
37 41
     private boolean isBottomOverscroll(int scrollY) {
38 42
         return scrollY >= (view.getChildAt(0).getHeight() - view.getHeight());
39 43
     }

+ 72
- 0
android/app/src/main/java/com/reactnativenavigation/views/ScrollViewAttacher.java View File

@@ -0,0 +1,72 @@
1
+package com.reactnativenavigation.views;
2
+
3
+import android.view.View;
4
+import android.view.ViewGroup;
5
+import android.widget.ScrollView;
6
+
7
+import com.reactnativenavigation.utils.ViewUtils;
8
+
9
+public class ScrollViewAttacher implements View.OnAttachStateChangeListener {
10
+
11
+    private final ContentView view;
12
+    private final ScrollDirectionListener.OnChanged onChanged;
13
+    private ScrollView scrollView;
14
+    private ScrollDirectionListener scrollDirectionListener;
15
+
16
+    public ScrollViewAttacher(ContentView view, ScrollDirectionListener.OnChanged onChanged) {
17
+        this.view = view;
18
+        this.onChanged = onChanged;
19
+    }
20
+
21
+    public void attach() {
22
+        ViewUtils.runOnPreDraw(view, new Runnable() {
23
+            @Override
24
+            public void run() {
25
+                findScrollAndStartListening();
26
+            }
27
+        });
28
+    }
29
+
30
+    public void detach() {
31
+        if (scrollView != null) {
32
+            scrollView.removeOnAttachStateChangeListener(this);
33
+            scrollView.getViewTreeObserver().removeOnScrollChangedListener(scrollDirectionListener);
34
+            scrollDirectionListener = null;
35
+            scrollView = null;
36
+        }
37
+    }
38
+
39
+    private void findScrollAndStartListening() {
40
+        scrollView = findScrollView(view);
41
+        if (scrollView != null) {
42
+            scrollDirectionListener = new ScrollDirectionListener(scrollView, onChanged);
43
+            scrollView.getViewTreeObserver().addOnScrollChangedListener(scrollDirectionListener);
44
+            scrollView.addOnAttachStateChangeListener(this);
45
+        }
46
+    }
47
+
48
+    private ScrollView findScrollView(ViewGroup parent) {
49
+        for (int i = 0; i < parent.getChildCount(); i++) {
50
+            View child = parent.getChildAt(i);
51
+
52
+            if (child instanceof ScrollView) {
53
+                return (ScrollView) child;
54
+            } else if (child instanceof ViewGroup) {
55
+                return findScrollView((ViewGroup) child);
56
+            }
57
+        }
58
+
59
+        return null;
60
+    }
61
+
62
+    @Override
63
+    public void onViewAttachedToWindow(View v) {
64
+        // nothing
65
+    }
66
+
67
+    @Override
68
+    public void onViewDetachedFromWindow(View v) {
69
+        detach();
70
+        attach();
71
+    }
72
+}