fix(core): Scroll listener register failure after unregister (#10368)

This commit is contained in:
Dimitris-Rafail Katsampas
2023-08-26 01:23:17 +03:00
committed by GitHub
parent f5f4666e04
commit e4fe276bed
3 changed files with 33 additions and 35 deletions

View File

@ -130,10 +130,10 @@ export class ScrollView extends ScrollViewBase {
protected attachNative() { protected attachNative() {
if (!this.handler) { if (!this.handler) {
const that = new WeakRef(this); const viewRef = new WeakRef(this);
this.handler = new android.view.ViewTreeObserver.OnScrollChangedListener({ this.handler = new android.view.ViewTreeObserver.OnScrollChangedListener({
onScrollChanged: function () { onScrollChanged: function () {
const owner: ScrollView = that.get(); const owner: ScrollView = viewRef.get();
if (owner) { if (owner) {
owner._onScrollChanged(); owner._onScrollChanged();
} }
@ -166,9 +166,13 @@ export class ScrollView extends ScrollViewBase {
} }
} }
protected dettachNative() { protected detachNative() {
this.nativeViewProtected.getViewTreeObserver().removeOnScrollChangedListener(this.handler); if (this.handler) {
this.handler = null; if (this.nativeViewProtected) {
this.nativeViewProtected.getViewTreeObserver().removeOnScrollChangedListener(this.handler);
}
this.handler = null;
}
} }
} }

View File

@ -49,12 +49,6 @@ export class ScrollView extends ScrollViewBase {
this._setNativeClipToBounds(); this._setNativeClipToBounds();
} }
disposeNativeView() {
this.dettachNative();
this._delegate = null;
super.disposeNativeView();
}
_setNativeClipToBounds() { _setNativeClipToBounds() {
if (!this.nativeViewProtected) { if (!this.nativeViewProtected) {
return; return;
@ -70,9 +64,12 @@ export class ScrollView extends ScrollViewBase {
} }
} }
protected dettachNative() { protected detachNative() {
if (this.nativeViewProtected) { if (this._delegate) {
this.nativeViewProtected.delegate = null; if (this.nativeViewProtected) {
this.nativeViewProtected.delegate = null;
}
this._delegate = null;
} }
} }

View File

@ -21,7 +21,9 @@ export abstract class ScrollViewBase extends ContentView implements ScrollViewDe
if (arg === ScrollViewBase.scrollEvent) { if (arg === ScrollViewBase.scrollEvent) {
this._scrollChangeCount++; 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); super.removeEventListener(arg, callback, thisArg);
if (arg === ScrollViewBase.scrollEvent) { if (arg === ScrollViewBase.scrollEvent) {
this._scrollChangeCount--; if (this._scrollChangeCount > 0) {
this.dettach(); this._scrollChangeCount--;
if (this.nativeViewProtected && this._scrollChangeCount === 0) {
this.detachNative();
}
}
} }
} }
@profile initNativeView() {
public onLoaded() { super.initNativeView();
super.onLoaded(); if (this._scrollChangeCount > 0) {
this.attach();
}
public disposeNativeView() {
this.dettach();
super.disposeNativeView();
}
private attach() {
if (this._scrollChangeCount > 0 && this.isLoaded) {
this.attachNative(); this.attachNative();
} }
} }
private dettach() { public disposeNativeView() {
if (this._scrollChangeCount === 0 && this.isLoaded) { if (this._scrollChangeCount > 0) {
this.dettachNative(); this.detachNative();
} }
super.disposeNativeView();
} }
protected attachNative() { protected attachNative() {
// //
} }
protected dettachNative() { protected detachNative() {
// //
} }