|
@@ -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
|
}
|