Skip to content

Commit ec43926

Browse files
authored
Merge pull request #85 from nativescript-community/add-findLastVisibleIndex-and-fix-ios-findFirstVisibleItemIndex
Add findLastVisibleItemIndex and fix ios findFirstVisibleItemIndex
2 parents 6f7d0ea + eb536db commit ec43926

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/collectionview/index-common.ts

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

src/collectionview/index.android.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,17 @@ export class CollectionView extends CollectionViewBase {
979979
}
980980
return -1;
981981
}
982+
public findLastVisibleItemIndex() {
983+
const view = this.nativeViewProtected;
984+
if (!view) {
985+
return -1;
986+
}
987+
const layoutManager = this.layoutManager as androidx.recyclerview.widget.LinearLayoutManager;
988+
if (layoutManager['findLastVisibleItemPosition']) {
989+
return layoutManager.findLastVisibleItemPosition();
990+
}
991+
return -1;
992+
}
982993

983994
_layedOut = false;
984995
@profile

src/collectionview/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class CollectionView extends CollectionViewBase {
1515
public refreshVisibleItems();
1616
public isItemAtIndexVisible(index: number): boolean;
1717
public findFirstVisibleItemIndex(): number;
18+
public findLastVisibleItemIndex(): number;
1819
public scrollToIndex(index: number, animated?: boolean, snap?: SnapPosition = SnapPosition.START);
1920
public scrollToOffset(value: number, animation?: boolean);
2021
public getViewForItemAtIndex(index: number): View;

src/collectionview/index.ios.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,11 +679,23 @@ export class CollectionView extends CollectionViewBase {
679679
if (!view) {
680680
return -1;
681681
}
682-
const indexes = Array.from<NSIndexPath>(view.indexPathsForVisibleItems)
682+
683+
return this.getRowIndexPath(view)[0] ?? -1;
684+
}
685+
public findLastVisibleItemIndex() {
686+
const view = this.nativeViewProtected;
687+
if (!view) {
688+
return -1;
689+
}
690+
return this.getRowIndexPath(view).at(-1) ?? -1;
691+
}
692+
693+
private getRowIndexPath(view: UICollectionView){
694+
return Array.from<NSIndexPath>(view.indexPathsForVisibleItems)
683695
.map((e) => e.row)
684-
.sort();
685-
return indexes[0] ?? -1;
696+
.sort((a, b) => a - b);
686697
}
698+
687699
@profile
688700
public refresh() {
689701
if (!this.isLoaded || !this.nativeView) {

0 commit comments

Comments
 (0)