Skip to content

Commit 8b5afa8

Browse files
committed
Fix: Multiple ScrollViews on one page fires scrollEvent simultaneously
Resolves NativeScript#2362
1 parent b9dca4f commit 8b5afa8

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

tns-core-modules/ui/scroll-view/scroll-view.android.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export class ScrollView extends common.ScrollView implements definition.ScrollVi
1414
private _android: org.nativescript.widgets.VerticalScrollView | org.nativescript.widgets.HorizontalScrollView;
1515
private _androidViewId: number = -1;
1616
private handler: android.view.ViewTreeObserver.OnScrollChangedListener;
17-
1817
get android(): android.view.ViewGroup {
1918
return this._android;
2019
}
@@ -111,20 +110,36 @@ export class ScrollView extends common.ScrollView implements definition.ScrollVi
111110
var that = new WeakRef(this);
112111
this.handler = new android.view.ViewTreeObserver.OnScrollChangedListener({
113112
onScrollChanged: function () {
114-
var rootScrollView = that.get();
115-
if (rootScrollView && rootScrollView.android) {
116-
rootScrollView.notify(<definition.ScrollEventData>{
117-
object: rootScrollView,
118-
eventName: ScrollView.scrollEvent,
119-
scrollX: rootScrollView.android.getScrollX() / utils.layout.getDisplayDensity(),
120-
scrollY: rootScrollView.android.getScrollY() / utils.layout.getDisplayDensity()
121-
});
113+
var owner: ScrollView = that.get();
114+
if (owner){
115+
owner._onScrollChanged();
122116
}
123117
}
124118
});
125119

126120
this._android.getViewTreeObserver().addOnScrollChangedListener(this.handler);
127121
}
122+
123+
private _lastScrollX: number = -1;
124+
private _lastScrollY: number = -1;
125+
private _onScrollChanged(){
126+
if (this.android) {
127+
// Event is only raised if the scroll values differ from the last time in order to wokraround a native Android bug.
128+
// https://github.com/NativeScript/NativeScript/issues/2362
129+
let newScrollX = this.android.getScrollX();
130+
let newScrollY = this.android.getScrollY();
131+
if (newScrollX !== this._lastScrollX || newScrollY !== this._lastScrollY){
132+
this.notify(<definition.ScrollEventData>{
133+
object: this,
134+
eventName: ScrollView.scrollEvent,
135+
scrollX: newScrollX / utils.layout.getDisplayDensity(),
136+
scrollY: newScrollY / utils.layout.getDisplayDensity()
137+
});
138+
this._lastScrollX = newScrollX;
139+
this._lastScrollY = newScrollY;
140+
}
141+
}
142+
}
128143

129144
protected dettachNative() {
130145
this._android.getViewTreeObserver().removeOnScrollChangedListener(this.handler);

0 commit comments

Comments
 (0)