From 1fac718a58d571f52c2e99c6c67dd21070b5d2e6 Mon Sep 17 00:00:00 2001 From: Shiva Prasad Date: Thu, 19 Jul 2018 19:10:59 +0530 Subject: [PATCH] feat(UI): Added animated scroll to index for ListView (#6077) * feat(UI): Added animated scroll to index for ListView * Update list-view.d.ts --- tns-core-modules/ui/list-view/list-view-common.ts | 4 ++++ tns-core-modules/ui/list-view/list-view.android.ts | 7 +++++++ tns-core-modules/ui/list-view/list-view.d.ts | 8 ++++++++ tns-core-modules/ui/list-view/list-view.ios.ts | 7 +++++++ 4 files changed, 26 insertions(+) diff --git a/tns-core-modules/ui/list-view/list-view-common.ts b/tns-core-modules/ui/list-view/list-view-common.ts index 6f468aa01..447fc09ef 100644 --- a/tns-core-modules/ui/list-view/list-view-common.ts +++ b/tns-core-modules/ui/list-view/list-view-common.ts @@ -90,6 +90,10 @@ export abstract class ListViewBase extends View implements ListViewDefinition, T // } + public scrollToIndexAnimated(index: number) { + // + } + public _getItemTemplate(index: number): KeyedTemplate { let templateKey = "default"; if (this.itemTemplateSelector) { diff --git a/tns-core-modules/ui/list-view/list-view.android.ts b/tns-core-modules/ui/list-view/list-view.android.ts index 8cc6528f0..46023cf5a 100644 --- a/tns-core-modules/ui/list-view/list-view.android.ts +++ b/tns-core-modules/ui/list-view/list-view.android.ts @@ -124,6 +124,13 @@ export class ListView extends ListViewBase { } } + public scrollToIndexAnimated(index: number) { + const nativeView = this.nativeViewProtected; + if (nativeView) { + nativeView.smoothScrollToPosition(index); + } + } + get _childrenCount(): number { return this._realizedItems.size; } diff --git a/tns-core-modules/ui/list-view/list-view.d.ts b/tns-core-modules/ui/list-view/list-view.d.ts index 08db3a021..b64181693 100644 --- a/tns-core-modules/ui/list-view/list-view.d.ts +++ b/tns-core-modules/ui/list-view/list-view.d.ts @@ -97,6 +97,14 @@ export class ListView extends View { */ scrollToIndex(index: number); + /** + * Scrolls the specified item with index into view with animation. + * [iOS](https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instm/UITableView/scrollToRowAtIndexPath:atScrollPosition:animated:) + * [Android](https://developer.android.com/reference/android/widget/ListView.html#smoothScrollToPosition(int)) + * @param index - Item index. + */ + scrollToIndexAnimated(index: number); + /** * Checks if Specified item with index is visible. * @param index - Item index. diff --git a/tns-core-modules/ui/list-view/list-view.ios.ts b/tns-core-modules/ui/list-view/list-view.ios.ts index 35b933327..8417d8682 100644 --- a/tns-core-modules/ui/list-view/list-view.ios.ts +++ b/tns-core-modules/ui/list-view/list-view.ios.ts @@ -261,6 +261,13 @@ export class ListView extends ListViewBase { } } + public scrollToIndexAnimated(index: number) { + if (this._ios) { + this._ios.scrollToRowAtIndexPathAtScrollPositionAnimated(NSIndexPath.indexPathForItemInSection(index, 0), + UITableViewScrollPosition.Top, true); + } + } + public refresh() { // clear bindingContext when it is not observable because otherwise bindings to items won't reevaluate this._map.forEach((view, nativeView, map) => {