Skip to content

Commit 76db3c8

Browse files
author
farfromrefug
committed
feat: new findFirstVisibleItemIndex method
1 parent 4e58df8 commit 76db3c8

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/collectionview/index-common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ export abstract class CollectionViewBase extends View implements CollectionViewD
179179
public abstract eachChildAsync(callback);
180180
public abstract refreshVisibleItems();
181181
public abstract isItemAtIndexVisible(index: number);
182+
public abstract findFirstVisibleItemIndex(): number;
182183
public abstract scrollToIndex(index: number, animated: boolean);
183184
public abstract scrollToOffset(value: number, animated?: boolean): any;
184185

src/collectionview/index.android.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,17 @@ export class CollectionView extends CollectionViewBase {
968968
}
969969
return false;
970970
}
971+
public findFirstVisibleItemIndex() {
972+
const view = this.nativeViewProtected;
973+
if (!view) {
974+
return -1;
975+
}
976+
const layoutManager = this.layoutManager as androidx.recyclerview.widget.LinearLayoutManager;
977+
if (layoutManager['findFirstVisibleItemPosition']) {
978+
return layoutManager.findFirstVisibleItemPosition();
979+
}
980+
return -1;
981+
}
971982

972983
_layedOut = false;
973984
@profile

src/collectionview/index.ios.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,13 @@ export class CollectionView extends CollectionViewBase {
666666

667667
return indexes.some((visIndex) => visIndex.row === itemIndex);
668668
}
669-
669+
public findFirstVisibleItemIndex() {
670+
const view = this.nativeViewProtected;
671+
if (!view) {
672+
return -1;
673+
}
674+
return view.indexPathsForVisibleItems?.objectAtIndex(0)?.row ?? -1;
675+
}
670676
@profile
671677
public refresh() {
672678
if (!this.isLoaded || !this.nativeView) {

0 commit comments

Comments
 (0)