Skip to content

Commit 1dd529a

Browse files
committed
feat: scroll and scrollEnd now reports scrollOffsetPercentage
1 parent 6676d95 commit 1dd529a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/collectionview/collectionview.android.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ export class CollectionView extends CollectionViewBase {
306306
inter
307307
? new com.nativescript.collectionview.SpanSizeLookup(
308308
new com.nativescript.collectionview.SpanSizeLookup.Interface({
309-
getSpanSize: (position)=>{
309+
getSpanSize: (position) => {
310310
const dataItem = this.getItemAtIndex(position);
311311
return inter(dataItem, position);
312312
}
@@ -356,10 +356,14 @@ export class CollectionView extends CollectionViewBase {
356356
}
357357

358358
if (this.hasListeners(CollectionViewBase.scrollEvent)) {
359+
const offset = this.isHorizontal() ? view.computeHorizontalScrollOffset() : view.computeVerticalScrollOffset();
360+
const range = view.computeHorizontalScrollRange();
361+
const extent = view.computeHorizontalScrollExtent();
359362
this.notify({
360363
object: this,
361364
eventName: CollectionViewBase.scrollEvent,
362-
scrollOffset: (this.isHorizontal() ? view.computeHorizontalScrollOffset() : view.computeVerticalScrollOffset()) / layout.getDisplayDensity()
365+
scrollOffset: offset / layout.getDisplayDensity(),
366+
scrollOffsetPercentage: offset / (range - extent)
363367
});
364368
}
365369

@@ -384,10 +388,14 @@ export class CollectionView extends CollectionViewBase {
384388
this.scrolling = false;
385389

386390
if (this.hasListeners(CollectionViewBase.scrollEndEvent)) {
391+
const offset = this.isHorizontal() ? view.computeHorizontalScrollOffset() : view.computeVerticalScrollOffset();
392+
const range = view.computeHorizontalScrollRange();
393+
const extent = view.computeHorizontalScrollExtent();
387394
this.notify({
388395
object: this,
389396
eventName: CollectionViewBase.scrollEndEvent,
390-
scrollOffset: (this.isHorizontal() ? view.computeHorizontalScrollOffset() : view.computeVerticalScrollOffset()) / layout.getDisplayDensity()
397+
scrollOffset: offset / layout.getDisplayDensity(),
398+
scrollOffsetPercentage: offset / (range - extent)
391399
});
392400
}
393401
} else if (!this.scrolling && newState === 1) {

src/collectionview/collectionview.ios.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,17 +885,23 @@ export class CollectionView extends CollectionViewBase {
885885
return CGSizeZero;
886886
}
887887
scrollViewDidScroll(scrollView: UIScrollView): void {
888+
const offset = this.isHorizontal() ? scrollView.contentOffset.x : scrollView.contentOffset.y;
889+
const size = this.isHorizontal() ? scrollView.contentSize.width : scrollView.contentSize.height;
888890
this.notify({
889891
object: this,
890892
eventName: CollectionViewBase.scrollEvent,
891-
scrollOffset: this.isHorizontal() ? scrollView.contentOffset.x : scrollView.contentOffset.y
893+
scrollOffset: offset,
894+
scrollOffsetPercentage: offset / size
892895
});
893896
}
894897
scrollViewDidEndDecelerating(scrollView: UIScrollView) {
898+
const offset = this.isHorizontal() ? scrollView.contentOffset.x : scrollView.contentOffset.y;
899+
const size = this.isHorizontal() ? scrollView.contentSize.width : scrollView.contentSize.height;
895900
this.notify({
896901
object: this,
897902
eventName: CollectionViewBase.scrollEndEvent,
898-
scrollOffset: this.isHorizontal() ? scrollView.contentOffset.x : scrollView.contentOffset.y
903+
scrollOffset: offset,
904+
scrollOffsetPercentage: offset / size
899905
});
900906
}
901907
scrollViewWillEndDraggingWithVelocityTargetContentOffset?(scrollView: UIScrollView, velocity: CGPoint, targetContentOffset: interop.Pointer | interop.Reference<CGPoint>): void {

0 commit comments

Comments
 (0)