Skip to content

Commit

Permalink
feat: isItemAtIndexVisible
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug committed May 19, 2021
1 parent 24fba9a commit 8c7239a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/collectionview/collectionview-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export abstract class CollectionViewBase extends View implements CollectionViewD

public abstract refresh();
public abstract refreshVisibleItems();
public abstract isItemAtIndexVisible(index:number);
public abstract scrollToIndex(index: number, animated: boolean);
public onLayout(left: number, top: number, right: number, bottom: number) {
super.onLayout(left, top, right, bottom);
Expand Down
13 changes: 13 additions & 0 deletions src/collectionview/collectionview.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,19 @@ export class CollectionView extends CollectionViewBase {
this._listViewAdapter.notifyItemRangeChanged(first, last - first + 1);
}
}
public isItemAtIndexVisible(index: number): boolean {
const view = this.nativeViewProtected;
if (!view) {
return false;
}
const layoutManager = this.layoutManager as androidx.recyclerview.widget.LinearLayoutManager;
if (layoutManager['findFirstVisibleItemPosition']) {
const first = layoutManager.findFirstVisibleItemPosition();
const last = layoutManager.findLastVisibleItemPosition();
return index >= first && index <= last;
}
return false;
}

@profile
public refresh() {
Expand Down
1 change: 1 addition & 0 deletions src/collectionview/collectionview.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Orientation = 'horizontal' | 'vertical';
export class CollectionView extends CollectionViewBase {
public refresh();
public refreshVisibleItems();
public isItemAtIndexVisible(index: number): boolean;
public scrollToIndex(index: number, animated: boolean);
public getViewForItemAtIndex(index: number): View;
// on iOS a view is dragged from its center by default
Expand Down
9 changes: 9 additions & 0 deletions src/collectionview/collectionview.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,15 @@ export class CollectionView extends CollectionViewBase {
}, null);
});
}
public isItemAtIndexVisible(itemIndex: number): boolean {
const view = this.nativeViewProtected;
if (!view) {
return false;
}
const indexes: NSIndexPath[] = Array.from(view.indexPathsForVisibleItems);

return indexes.some((visIndex) => visIndex.row === itemIndex);
}

@profile
public refresh() {
Expand Down

0 comments on commit 8c7239a

Please sign in to comment.