diff --git a/packages/core/ui/scroll-view/index.android.ts b/packages/core/ui/scroll-view/index.android.ts index 153fdbc50..5b0b2b1e6 100644 --- a/packages/core/ui/scroll-view/index.android.ts +++ b/packages/core/ui/scroll-view/index.android.ts @@ -130,10 +130,10 @@ export class ScrollView extends ScrollViewBase { protected attachNative() { if (!this.handler) { - const that = new WeakRef(this); + const viewRef = new WeakRef(this); this.handler = new android.view.ViewTreeObserver.OnScrollChangedListener({ onScrollChanged: function () { - const owner: ScrollView = that.get(); + const owner: ScrollView = viewRef.get(); if (owner) { owner._onScrollChanged(); } @@ -166,9 +166,13 @@ export class ScrollView extends ScrollViewBase { } } - protected dettachNative() { - this.nativeViewProtected.getViewTreeObserver().removeOnScrollChangedListener(this.handler); - this.handler = null; + protected detachNative() { + if (this.handler) { + if (this.nativeViewProtected) { + this.nativeViewProtected.getViewTreeObserver().removeOnScrollChangedListener(this.handler); + } + this.handler = null; + } } } diff --git a/packages/core/ui/scroll-view/index.ios.ts b/packages/core/ui/scroll-view/index.ios.ts index e4d5c4894..984339695 100644 --- a/packages/core/ui/scroll-view/index.ios.ts +++ b/packages/core/ui/scroll-view/index.ios.ts @@ -49,12 +49,6 @@ export class ScrollView extends ScrollViewBase { this._setNativeClipToBounds(); } - disposeNativeView() { - this.dettachNative(); - this._delegate = null; - super.disposeNativeView(); - } - _setNativeClipToBounds() { if (!this.nativeViewProtected) { return; @@ -70,9 +64,12 @@ export class ScrollView extends ScrollViewBase { } } - protected dettachNative() { - if (this.nativeViewProtected) { - this.nativeViewProtected.delegate = null; + protected detachNative() { + if (this._delegate) { + if (this.nativeViewProtected) { + this.nativeViewProtected.delegate = null; + } + this._delegate = null; } } diff --git a/packages/core/ui/scroll-view/scroll-view-common.ts b/packages/core/ui/scroll-view/scroll-view-common.ts index 6e4e39806..206b366ad 100644 --- a/packages/core/ui/scroll-view/scroll-view-common.ts +++ b/packages/core/ui/scroll-view/scroll-view-common.ts @@ -21,7 +21,9 @@ export abstract class ScrollViewBase extends ContentView implements ScrollViewDe if (arg === ScrollViewBase.scrollEvent) { this._scrollChangeCount++; - this.attach(); + if (this.nativeViewProtected) { + this.attachNative(); + } } } @@ -29,40 +31,35 @@ export abstract class ScrollViewBase extends ContentView implements ScrollViewDe super.removeEventListener(arg, callback, thisArg); if (arg === ScrollViewBase.scrollEvent) { - this._scrollChangeCount--; - this.dettach(); + if (this._scrollChangeCount > 0) { + this._scrollChangeCount--; + + if (this.nativeViewProtected && this._scrollChangeCount === 0) { + this.detachNative(); + } + } } } - @profile - public onLoaded() { - super.onLoaded(); - - this.attach(); - } - - public disposeNativeView() { - this.dettach(); - super.disposeNativeView(); - } - - private attach() { - if (this._scrollChangeCount > 0 && this.isLoaded) { + initNativeView() { + super.initNativeView(); + if (this._scrollChangeCount > 0) { this.attachNative(); } } - private dettach() { - if (this._scrollChangeCount === 0 && this.isLoaded) { - this.dettachNative(); + public disposeNativeView() { + if (this._scrollChangeCount > 0) { + this.detachNative(); } + super.disposeNativeView(); } protected attachNative() { // } - protected dettachNative() { + protected detachNative() { // }