| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -759,6 +759,33 @@ export class ListViewTest extends UITest<ListView> { | |||
| 759 | 759 | TKUnit.assertEqual(lastNativeElementVisible, false, "Last element is not visible"); | |
| 760 | 760 | } | |
| 761 | 761 | ||
| 762 | + public test_scrollToIndex_should_coerce_negative_index_to_zero_index() { | ||
| 763 | + var listView = this.testView; | ||
| 764 | + | ||
| 765 | + listView.items = MANY_ITEMS; | ||
| 766 | + listView.scrollToIndex(-1); | ||
| 767 | + TKUnit.wait(0.1); | ||
| 768 | + | ||
| 769 | + var firstNativeElementVisible = this.checkItemVisibleAtIndex(listView, 0); | ||
| 770 | + TKUnit.assertEqual(firstNativeElementVisible, true, "first element is visible"); | ||
| 771 | + } | ||
| 772 | + | ||
| 773 | + public test_scrollToIndex_should_coerce_larger_index_to_last_item_index() { | ||
| 774 | + var listView = this.testView; | ||
| 775 | + | ||
| 776 | + listView.items = MANY_ITEMS; | ||
| 777 | + listView.scrollToIndex(10000); | ||
| 778 | + TKUnit.wait(0.1); | ||
| 779 | + | ||
| 780 | + var lastNativeElementVisible = this.checkItemVisibleAtIndex(listView, MANY_ITEMS.length - 1); | ||
| 781 | + TKUnit.assertEqual(lastNativeElementVisible, true, "last element is visible"); | ||
| 782 | + } | ||
| 783 | + | ||
| 784 | + public test_scrollToIndex_should_not_throw_if_items_not_set() { | ||
| 785 | + var listView = this.testView; | ||
| 786 | + listView.scrollToIndex(10000); | ||
| 787 | + } | ||
| 788 | + | ||
| 762 | 789 | private checkItemVisibleAtIndex(listView: ListView, index: number): boolean { | |
| 763 | 790 | return listView.isItemAtIndexVisible(index); | |
| 764 | 791 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,11 +1,12 @@ | |||
| 1 | - import { ItemEventData } from "."; | ||
| 1 | + import { ItemEventData } from "."; | ||
| 2 | 2 | import { | |
| 3 | 3 | ListViewBase, View, KeyedTemplate, Length, Observable, Color, | |
| 4 | 4 | separatorColorProperty, itemTemplatesProperty, iosEstimatedRowHeightProperty, layout, EventData | |
| 5 | 5 | } from "./list-view-common"; | |
| 6 | 6 | import { StackLayout } from "../layouts/stack-layout"; | |
| 7 | 7 | import { ProxyViewContainer } from "../proxy-view-container"; | |
| 8 | 8 | import { profile } from "../../profiling"; | |
| 9 | + import * as trace from "../../trace"; | ||
| 9 | 10 | ||
| 10 | 11 | export * from "./list-view-common"; | |
| 11 | 12 | ||
@@ -261,16 +262,31 @@ export class ListView extends ListViewBase { | |||
| 261 | 262 | } | |
| 262 | 263 | ||
| 263 | 264 | public scrollToIndex(index: number) { | |
| 264 | - if (this._ios) { | ||
| 265 | - this._ios.scrollToRowAtIndexPathAtScrollPositionAnimated(NSIndexPath.indexPathForItemInSection(index, 0), | ||
| 266 | - UITableViewScrollPosition.Top, false); | ||
| 267 | - } | ||
| 265 | + this._scrollToIndex(index, false); | ||
| 268 | 266 | } | |
| 269 | 267 | ||
| 270 | 268 | public scrollToIndexAnimated(index: number) { | |
| 271 | - if (this._ios) { | ||
| 269 | + this._scrollToIndex(index); | ||
| 270 | + } | ||
| 271 | + | ||
| 272 | + private _scrollToIndex(index: number, animated: boolean = true) { | ||
| 273 | + if (!this._ios) { | ||
| 274 | + return; | ||
| 275 | + } | ||
| 276 | + | ||
| 277 | + const itemsLength = this.items ? this.items.length : 0; | ||
| 278 | + // mimic Android behavior that silently coerces index values within [0, itemsLength - 1] range | ||
| 279 | + if (itemsLength > 0) { | ||
| 280 | + if (index < 0) { | ||
| 281 | + index = 0 | ||
| 282 | + } else if (index >= itemsLength) { | ||
| 283 | + index = itemsLength - 1; | ||
| 284 | + } | ||
| 285 | + | ||
| 272 | 286 | this._ios.scrollToRowAtIndexPathAtScrollPositionAnimated(NSIndexPath.indexPathForItemInSection(index, 0), | |
| 273 | - UITableViewScrollPosition.Top, true); | ||
| 287 | + UITableViewScrollPosition.Top, animated); | ||
| 288 | + } else if (trace.isEnabled()) { | ||
| 289 | + trace.write(`Cannot scroll listview to index ${index} when listview items not set`, trace.categories.Binding); | ||
| 274 | 290 | } | |
| 275 | 291 | } | |
| 276 | 292 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments