mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat(List-View) : Add the ability to check if an item is visible or not. (#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.
This commit is contained in:
committed by
Alexander Vakrilov
parent
7506905770
commit
4b244921d4
@@ -705,6 +705,26 @@ export class ListViewTest extends testModule.UITest<listViewModule.ListView> {
|
|||||||
TKUnit.assert(weakRef.get(), weakRef.get() + " died prematurely!");
|
TKUnit.assert(weakRef.get(), weakRef.get() + " died prematurely!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public test_check_if_item_at_index_is_visible() {
|
||||||
|
var listView = this.testView;
|
||||||
|
|
||||||
|
listView.itemTemplate = "<Label text='default' minHeight='100' maxHeight='100'/>";
|
||||||
|
listView.items = ListViewTest.generateItemsForMultipleTemplatesTests(40);
|
||||||
|
TKUnit.wait(0.1);
|
||||||
|
|
||||||
|
var firstNativeElementVisible = this.checkItemVisibleAtIndex(listView, 0);
|
||||||
|
var secondNativeElementVisible = this.checkItemVisibleAtIndex(listView, 1);
|
||||||
|
var lastNativeElementVisible = this.checkItemVisibleAtIndex(listView, 39);
|
||||||
|
|
||||||
|
TKUnit.assertEqual(firstNativeElementVisible, true, "first element is visible");
|
||||||
|
TKUnit.assertEqual(secondNativeElementVisible, true, "second element is visible");
|
||||||
|
TKUnit.assertEqual(lastNativeElementVisible, false, "Last element is not visible");
|
||||||
|
}
|
||||||
|
|
||||||
|
private checkItemVisibleAtIndex(listView: listViewModule.ListView, index: number ): boolean {
|
||||||
|
return listView.isItemAtIndexVisible(index);
|
||||||
|
}
|
||||||
|
|
||||||
private assertNoMemoryLeak(weakRef: WeakRef<listViewModule.ListView>) {
|
private assertNoMemoryLeak(weakRef: WeakRef<listViewModule.ListView>) {
|
||||||
this.tearDown();
|
this.tearDown();
|
||||||
TKUnit.waitUntilReady(() => {
|
TKUnit.waitUntilReady(() => {
|
||||||
|
|||||||
@@ -77,7 +77,6 @@ export abstract class ListViewBase extends View implements ListViewDefinition {
|
|||||||
get itemIdGenerator(): (item: any, index: number, items: any) => number {
|
get itemIdGenerator(): (item: any, index: number, items: any) => number {
|
||||||
return this._itemIdGenerator;
|
return this._itemIdGenerator;
|
||||||
}
|
}
|
||||||
|
|
||||||
set itemIdGenerator(generatorFn: (item: any, index: number, items: any) => number) {
|
set itemIdGenerator(generatorFn: (item: any, index: number, items: any) => number) {
|
||||||
this._itemIdGenerator = generatorFn;
|
this._itemIdGenerator = generatorFn;
|
||||||
}
|
}
|
||||||
@@ -135,6 +134,10 @@ export abstract class ListViewBase extends View implements ListViewDefinition {
|
|||||||
this.refresh();
|
this.refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public isItemAtIndexVisible(index: number) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
protected updateEffectiveRowHeight(): void {
|
protected updateEffectiveRowHeight(): void {
|
||||||
rowHeightProperty.coerce(this);
|
rowHeightProperty.coerce(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { StackLayout } from "../layouts/stack-layout";
|
|||||||
import { ProxyViewContainer } from "../proxy-view-container";
|
import { ProxyViewContainer } from "../proxy-view-container";
|
||||||
import { LayoutBase } from "../layouts/layout-base";
|
import { LayoutBase } from "../layouts/layout-base";
|
||||||
import { profile } from "../../profiling";
|
import { profile } from "../../profiling";
|
||||||
|
import { onScroll } from '../../../apps/app/ui-tests-app/list-view/list-view';
|
||||||
|
|
||||||
export * from "./list-view-common";
|
export * from "./list-view-common";
|
||||||
|
|
||||||
@@ -44,7 +45,6 @@ function initializeItemClickListener(): void {
|
|||||||
|
|
||||||
export class ListView extends ListViewBase {
|
export class ListView extends ListViewBase {
|
||||||
nativeViewProtected: android.widget.ListView;
|
nativeViewProtected: android.widget.ListView;
|
||||||
|
|
||||||
private _androidViewId: number = -1;
|
private _androidViewId: number = -1;
|
||||||
|
|
||||||
public _realizedItems = new Map<android.view.View, View>();
|
public _realizedItems = new Map<android.view.View, View>();
|
||||||
@@ -170,6 +170,13 @@ export class ListView extends ListViewBase {
|
|||||||
this._realizedTemplates.clear();
|
this._realizedTemplates.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public isItemAtIndexVisible(index: number): boolean {
|
||||||
|
let nativeView = this.nativeViewProtected;
|
||||||
|
const start = nativeView.getFirstVisiblePosition();
|
||||||
|
const end = nativeView.getLastVisiblePosition();
|
||||||
|
return ( index >= start && index <= end );
|
||||||
|
}
|
||||||
|
|
||||||
[separatorColorProperty.getDefault](): { dividerHeight: number, divider: android.graphics.drawable.Drawable } {
|
[separatorColorProperty.getDefault](): { dividerHeight: number, divider: android.graphics.drawable.Drawable } {
|
||||||
let nativeView = this.nativeViewProtected;
|
let nativeView = this.nativeViewProtected;
|
||||||
return {
|
return {
|
||||||
|
|||||||
6
tns-core-modules/ui/list-view/list-view.d.ts
vendored
6
tns-core-modules/ui/list-view/list-view.d.ts
vendored
@@ -91,6 +91,12 @@ export class ListView extends View {
|
|||||||
*/
|
*/
|
||||||
scrollToIndex(index: number);
|
scrollToIndex(index: number);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if Specified item with index is visible.
|
||||||
|
* @param index - Item index.
|
||||||
|
*/
|
||||||
|
isItemAtIndexVisible(index: number): boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
|
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
|
||||||
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
|
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
|
||||||
|
|||||||
@@ -285,6 +285,11 @@ export class ListView extends ListViewBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public isItemAtIndexVisible( itemIndex: number ): boolean {
|
||||||
|
const indexes: NSIndexPath[] = Array.from(this._ios.indexPathsForVisibleRows);
|
||||||
|
return indexes.some(visIndex => visIndex.row === itemIndex);
|
||||||
|
}
|
||||||
|
|
||||||
public getHeight(index: number): number {
|
public getHeight(index: number): number {
|
||||||
return this._heights[index];
|
return this._heights[index];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user