Pārlūkot izejas kodu

scroll view attacher clean up

Daniel Zlotin 8 gadus atpakaļ
vecāks
revīzija
f3e1c2be16

+ 9
- 3
android/app/src/main/java/com/reactnativenavigation/views/ContentView.java Parādīt failu

@@ -2,6 +2,7 @@ package com.reactnativenavigation.views;
2 2
 
3 3
 import android.content.Context;
4 4
 import android.os.Bundle;
5
+import android.view.View;
5 6
 
6 7
 import com.facebook.react.ReactInstanceManager;
7 8
 import com.facebook.react.ReactRootView;
@@ -14,20 +15,25 @@ public class ContentView extends ReactRootView {
14 15
     private final String screenId;
15 16
     private final Bundle passProps;
16 17
     private Bundle navigationParams;
17
-    private final ScrollDirectionListener.OnScrollChanged scrollListener;
18
+    private ScrollViewAttacher scrollViewAttacher;
18 19
 
19 20
     public ContentView(Context context, ScreenParams screenParams, ScrollDirectionListener.OnScrollChanged scrollListener) {
20 21
         super(context);
21 22
         this.screenId = screenParams.screenId;
22 23
         this.passProps = screenParams.passProps;
23 24
         this.navigationParams = screenParams.navigationParams;
24
-        this.scrollListener = scrollListener;
25
+        scrollViewAttacher = new ScrollViewAttacher(scrollListener);
25 26
     }
26 27
 
27 28
     public void init() {
28 29
         ReactInstanceManager react = NavigationApplication.instance.getNavigationReactInstance().getReactInstanceManager();
29 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 39
     private Bundle mergePropsAndNavigationParams() {

+ 9
- 51
android/app/src/main/java/com/reactnativenavigation/views/ScrollViewAttacher.java Parādīt failu

@@ -1,74 +1,32 @@
1 1
 package com.reactnativenavigation.views;
2 2
 
3
-import android.util.Log;
4 3
 import android.view.View;
5
-import android.view.ViewGroup;
6 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 8
     private final ScrollDirectionListener.OnScrollChanged onChanged;
14 9
     private ScrollView scrollView;
15 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 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 25
     public void detach() {
32 26
         if (scrollView != null) {
33
-            scrollView.removeOnAttachStateChangeListener(this);
34 27
             scrollView.getViewTreeObserver().removeOnScrollChangedListener(scrollDirectionListener);
35 28
             scrollDirectionListener = null;
36 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
 }