Browse Source

scroll view attacher clean up

Daniel Zlotin 8 years ago
parent
commit
f3e1c2be16

+ 9
- 3
android/app/src/main/java/com/reactnativenavigation/views/ContentView.java View File

2
 
2
 
3
 import android.content.Context;
3
 import android.content.Context;
4
 import android.os.Bundle;
4
 import android.os.Bundle;
5
+import android.view.View;
5
 
6
 
6
 import com.facebook.react.ReactInstanceManager;
7
 import com.facebook.react.ReactInstanceManager;
7
 import com.facebook.react.ReactRootView;
8
 import com.facebook.react.ReactRootView;
14
     private final String screenId;
15
     private final String screenId;
15
     private final Bundle passProps;
16
     private final Bundle passProps;
16
     private Bundle navigationParams;
17
     private Bundle navigationParams;
17
-    private final ScrollDirectionListener.OnScrollChanged scrollListener;
18
+    private ScrollViewAttacher scrollViewAttacher;
18
 
19
 
19
     public ContentView(Context context, ScreenParams screenParams, ScrollDirectionListener.OnScrollChanged scrollListener) {
20
     public ContentView(Context context, ScreenParams screenParams, ScrollDirectionListener.OnScrollChanged scrollListener) {
20
         super(context);
21
         super(context);
21
         this.screenId = screenParams.screenId;
22
         this.screenId = screenParams.screenId;
22
         this.passProps = screenParams.passProps;
23
         this.passProps = screenParams.passProps;
23
         this.navigationParams = screenParams.navigationParams;
24
         this.navigationParams = screenParams.navigationParams;
24
-        this.scrollListener = scrollListener;
25
+        scrollViewAttacher = new ScrollViewAttacher(scrollListener);
25
     }
26
     }
26
 
27
 
27
     public void init() {
28
     public void init() {
28
         ReactInstanceManager react = NavigationApplication.instance.getNavigationReactInstance().getReactInstanceManager();
29
         ReactInstanceManager react = NavigationApplication.instance.getNavigationReactInstance().getReactInstanceManager();
29
         startReactApplication(react, screenId, mergePropsAndNavigationParams());
30
         startReactApplication(react, screenId, mergePropsAndNavigationParams());
30
-        new ScrollViewAttacher(this, scrollListener).attach();
31
+    }
32
+
33
+    @Override
34
+    public void onViewAdded(View child) {
35
+        super.onViewAdded(child);
36
+        scrollViewAttacher.onViewAdded(child);
31
     }
37
     }
32
 
38
 
33
     private Bundle mergePropsAndNavigationParams() {
39
     private Bundle mergePropsAndNavigationParams() {

+ 9
- 51
android/app/src/main/java/com/reactnativenavigation/views/ScrollViewAttacher.java View File

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