Skip to content

Commit 4b24492

Browse files
Brendan InghamAlexander Vakrilov
authored andcommitted
feat(List-View) : Add the ability to check if an item is visible or not. (NativeScript#5557)
* feat(list-view): Adds the ability to check whether or not an Item at index is Visible on screen within a listView * feat(list-view): add the unit-test for checking if list-item is visible. * clean(list-view): fix invalid reference in list-view-common * chore(list-view): remove unused logic * test(list-view) updates the tests for checking if item at index is visible * chore(ListView Tests): update the test_check_if_item_at_index_is_visible unit test to include 40 children, and test if the last item is visible or not. * Chore(ListView IOS): Apply requested changes to the for-loop, and replace with Array.some for readability. * chore(ListView android): Fix TSLint issues.
1 parent 7506905 commit 4b24492

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

tests/app/ui/list-view/list-view-tests.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,26 @@ export class ListViewTest extends testModule.UITest<listViewModule.ListView> {
705705
TKUnit.assert(weakRef.get(), weakRef.get() + " died prematurely!");
706706
}
707707

708+
public test_check_if_item_at_index_is_visible() {
709+
var listView = this.testView;
710+
711+
listView.itemTemplate = "<Label text='default' minHeight='100' maxHeight='100'/>";
712+
listView.items = ListViewTest.generateItemsForMultipleTemplatesTests(40);
713+
TKUnit.wait(0.1);
714+
715+
var firstNativeElementVisible = this.checkItemVisibleAtIndex(listView, 0);
716+
var secondNativeElementVisible = this.checkItemVisibleAtIndex(listView, 1);
717+
var lastNativeElementVisible = this.checkItemVisibleAtIndex(listView, 39);
718+
719+
TKUnit.assertEqual(firstNativeElementVisible, true, "first element is visible");
720+
TKUnit.assertEqual(secondNativeElementVisible, true, "second element is visible");
721+
TKUnit.assertEqual(lastNativeElementVisible, false, "Last element is not visible");
722+
}
723+
724+
private checkItemVisibleAtIndex(listView: listViewModule.ListView, index: number ): boolean {
725+
return listView.isItemAtIndexVisible(index);
726+
}
727+
708728
private assertNoMemoryLeak(weakRef: WeakRef<listViewModule.ListView>) {
709729
this.tearDown();
710730
TKUnit.waitUntilReady(() => {

tns-core-modules/ui/list-view/list-view-common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ export abstract class ListViewBase extends View implements ListViewDefinition {
7777
get itemIdGenerator(): (item: any, index: number, items: any) => number {
7878
return this._itemIdGenerator;
7979
}
80-
8180
set itemIdGenerator(generatorFn: (item: any, index: number, items: any) => number) {
8281
this._itemIdGenerator = generatorFn;
8382
}
@@ -135,6 +134,10 @@ export abstract class ListViewBase extends View implements ListViewDefinition {
135134
this.refresh();
136135
}
137136

137+
public isItemAtIndexVisible(index: number) {
138+
return false;
139+
}
140+
138141
protected updateEffectiveRowHeight(): void {
139142
rowHeightProperty.coerce(this);
140143
}

tns-core-modules/ui/list-view/list-view.android.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { StackLayout } from "../layouts/stack-layout";
77
import { ProxyViewContainer } from "../proxy-view-container";
88
import { LayoutBase } from "../layouts/layout-base";
99
import { profile } from "../../profiling";
10+
import { onScroll } from '../../../apps/app/ui-tests-app/list-view/list-view';
1011

1112
export * from "./list-view-common";
1213

@@ -44,7 +45,6 @@ function initializeItemClickListener(): void {
4445

4546
export class ListView extends ListViewBase {
4647
nativeViewProtected: android.widget.ListView;
47-
4848
private _androidViewId: number = -1;
4949

5050
public _realizedItems = new Map<android.view.View, View>();
@@ -170,6 +170,13 @@ export class ListView extends ListViewBase {
170170
this._realizedTemplates.clear();
171171
}
172172

173+
public isItemAtIndexVisible(index: number): boolean {
174+
let nativeView = this.nativeViewProtected;
175+
const start = nativeView.getFirstVisiblePosition();
176+
const end = nativeView.getLastVisiblePosition();
177+
return ( index >= start && index <= end );
178+
}
179+
173180
[separatorColorProperty.getDefault](): { dividerHeight: number, divider: android.graphics.drawable.Drawable } {
174181
let nativeView = this.nativeViewProtected;
175182
return {

tns-core-modules/ui/list-view/list-view.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ export class ListView extends View {
9191
*/
9292
scrollToIndex(index: number);
9393

94+
/**
95+
* Checks if Specified item with index is visible.
96+
* @param index - Item index.
97+
*/
98+
isItemAtIndexVisible(index: number): boolean;
99+
94100
/**
95101
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
96102
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").

tns-core-modules/ui/list-view/list-view.ios.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ export class ListView extends ListViewBase {
285285
}
286286
}
287287

288+
public isItemAtIndexVisible( itemIndex: number ): boolean {
289+
const indexes: NSIndexPath[] = Array.from(this._ios.indexPathsForVisibleRows);
290+
return indexes.some(visIndex => visIndex.row === itemIndex);
291+
}
292+
288293
public getHeight(index: number): number {
289294
return this._heights[index];
290295
}

0 commit comments

Comments
 (0)