@@ -9,47 +9,53 @@ import { CoreTypes } from '../../core-types';
9
9
10
10
@CSSType ( 'ScrollView' )
11
11
export abstract class ScrollViewBase extends ContentView implements ScrollViewDefinition {
12
- private _scrollChangeCount = 0 ;
13
12
public static scrollEvent = 'scroll' ;
14
13
15
14
public orientation : CoreTypes . OrientationType ;
16
15
public scrollBarIndicatorVisible : boolean ;
17
16
public isScrollEnabled : boolean ;
18
17
18
+ private _addedScrollEvent = false ;
19
+
19
20
public addEventListener ( arg : string , callback : ( data : EventData ) => void , thisArg ?: any ) : void {
21
+ const hasExistingScrollListeners : boolean = this . hasListeners ( ScrollViewBase . scrollEvent ) ;
22
+
20
23
super . addEventListener ( arg , callback , thisArg ) ;
21
24
22
- if ( arg === ScrollViewBase . scrollEvent ) {
23
- this . _scrollChangeCount ++ ;
25
+ // This indicates that a scroll listener was added for first time
26
+ if ( ! hasExistingScrollListeners && this . hasListeners ( ScrollViewBase . scrollEvent ) ) {
27
+ this . _addedScrollEvent = true ;
28
+
24
29
if ( this . nativeViewProtected ) {
25
30
this . attachNative ( ) ;
26
31
}
27
32
}
28
33
}
29
34
30
35
public removeEventListener ( arg : string , callback ?: ( data : EventData ) => void , thisArg ?: any ) : void {
36
+ const hasExistingScrollListeners : boolean = this . hasListeners ( ScrollViewBase . scrollEvent ) ;
37
+
31
38
super . removeEventListener ( arg , callback , thisArg ) ;
32
39
33
- if ( arg === ScrollViewBase . scrollEvent ) {
34
- if ( this . _scrollChangeCount > 0 ) {
35
- this . _scrollChangeCount -- ;
40
+ // This indicates that the final scroll listener was removed
41
+ if ( hasExistingScrollListeners && ! this . hasListeners ( ScrollViewBase . scrollEvent ) ) {
42
+ this . _addedScrollEvent = false ;
36
43
37
- if ( this . nativeViewProtected && this . _scrollChangeCount === 0 ) {
38
- this . detachNative ( ) ;
39
- }
44
+ if ( this . nativeViewProtected ) {
45
+ this . detachNative ( ) ;
40
46
}
41
47
}
42
48
}
43
49
44
50
initNativeView ( ) {
45
51
super . initNativeView ( ) ;
46
- if ( this . _scrollChangeCount > 0 ) {
52
+ if ( this . _addedScrollEvent ) {
47
53
this . attachNative ( ) ;
48
54
}
49
55
}
50
56
51
57
public disposeNativeView ( ) {
52
- if ( this . _scrollChangeCount > 0 ) {
58
+ if ( this . _addedScrollEvent ) {
53
59
this . detachNative ( ) ;
54
60
}
55
61
super . disposeNativeView ( ) ;
0 commit comments