Skip to content

Commit 8c7239a

Browse files
committed
feat: isItemAtIndexVisible
1 parent 24fba9a commit 8c7239a

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

src/collectionview/collectionview-common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export abstract class CollectionViewBase extends View implements CollectionViewD
137137

138138
public abstract refresh();
139139
public abstract refreshVisibleItems();
140+
public abstract isItemAtIndexVisible(index:number);
140141
public abstract scrollToIndex(index: number, animated: boolean);
141142
public onLayout(left: number, top: number, right: number, bottom: number) {
142143
super.onLayout(left, top, right, bottom);

src/collectionview/collectionview.android.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,19 @@ export class CollectionView extends CollectionViewBase {
774774
this._listViewAdapter.notifyItemRangeChanged(first, last - first + 1);
775775
}
776776
}
777+
public isItemAtIndexVisible(index: number): boolean {
778+
const view = this.nativeViewProtected;
779+
if (!view) {
780+
return false;
781+
}
782+
const layoutManager = this.layoutManager as androidx.recyclerview.widget.LinearLayoutManager;
783+
if (layoutManager['findFirstVisibleItemPosition']) {
784+
const first = layoutManager.findFirstVisibleItemPosition();
785+
const last = layoutManager.findLastVisibleItemPosition();
786+
return index >= first && index <= last;
787+
}
788+
return false;
789+
}
777790

778791
@profile
779792
public refresh() {

src/collectionview/collectionview.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type Orientation = 'horizontal' | 'vertical';
88
export class CollectionView extends CollectionViewBase {
99
public refresh();
1010
public refreshVisibleItems();
11+
public isItemAtIndexVisible(index: number): boolean;
1112
public scrollToIndex(index: number, animated: boolean);
1213
public getViewForItemAtIndex(index: number): View;
1314
// on iOS a view is dragged from its center by default

src/collectionview/collectionview.ios.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,15 @@ export class CollectionView extends CollectionViewBase {
509509
}, null);
510510
});
511511
}
512+
public isItemAtIndexVisible(itemIndex: number): boolean {
513+
const view = this.nativeViewProtected;
514+
if (!view) {
515+
return false;
516+
}
517+
const indexes: NSIndexPath[] = Array.from(view.indexPathsForVisibleItems);
518+
519+
return indexes.some((visIndex) => visIndex.row === itemIndex);
520+
}
512521

513522
@profile
514523
public refresh() {

0 commit comments

Comments
 (0)