public static (int firstVisibleItemIndex, int lastVisibleItemIndex, int centerItemIndex) GetVisibleIndexes(ListViewBase listViewBase, ItemsLayoutOrientation itemsLayoutOrientation) { int firstVisibleItemIndex = -1; int lastVisibleItemIndex = -1; int centerItemIndex = -1; var scrollViewer = listViewBase.GetFirstDescendant <ScrollViewer>(); var presenters = listViewBase.GetChildren <ListViewItemPresenter>(); if (presenters != null || scrollViewer == null) { int count = 0; foreach (ListViewItemPresenter presenter in presenters) { if (CollectionViewExtensions.IsListViewItemVisible(presenter, scrollViewer, itemsLayoutOrientation)) { if (firstVisibleItemIndex == -1) { firstVisibleItemIndex = count; } lastVisibleItemIndex = count; } count++; } centerItemIndex = (lastVisibleItemIndex + firstVisibleItemIndex) / 2; } return(firstVisibleItemIndex, lastVisibleItemIndex, centerItemIndex); }
void OnScrollViewChanged(object sender, ScrollViewerViewChangedEventArgs e) { var itemsViewScrolledEventArgs = new ItemsViewScrolledEventArgs { HorizontalOffset = _scrollViewer.HorizontalOffset, HorizontalDelta = _scrollViewer.HorizontalOffset - _previousHorizontalOffset, VerticalOffset = _scrollViewer.VerticalOffset, VerticalDelta = _scrollViewer.VerticalOffset - _previousVerticalOffset, }; _previousHorizontalOffset = _scrollViewer.HorizontalOffset; _previousVerticalOffset = _scrollViewer.VerticalOffset; int firstVisibleItemIndex = -1; int lastVisibleItemIndex = -1; int firstVisibleSourceItemIndex = -1; List <object> sourceItems = GetSourceItems(); var presenters = ListViewBase.GetChildren <ListViewItemPresenter>(); if (presenters != null) { int count = 0; foreach (ListViewItemPresenter presenter in presenters) { if (IsListViewItemVisible(presenter, _scrollViewer)) { if (firstVisibleItemIndex == -1) { firstVisibleItemIndex = count; firstVisibleSourceItemIndex = GetFirstVisibleSourceItemIndex(sourceItems, presenter); } lastVisibleItemIndex = count; } count++; } itemsViewScrolledEventArgs.FirstVisibleItemIndex = firstVisibleItemIndex; itemsViewScrolledEventArgs.CenterItemIndex = (lastVisibleItemIndex + firstVisibleItemIndex) / 2; itemsViewScrolledEventArgs.LastVisibleItemIndex = lastVisibleItemIndex; } Element.SendScrolled(itemsViewScrolledEventArgs); int lastVisibleSourceItemIndex = GetLastVisibleSourceItemIndex(firstVisibleSourceItemIndex, firstVisibleItemIndex, lastVisibleItemIndex); Element.ResolveRemainingItemsThreshold(sourceItems.Count, lastVisibleSourceItemIndex); }
public static (int firstVisibleItemIndex, int lastVisibleItemIndex, int centerItemIndex) GetVisibleIndexes(this ListViewBase listViewBase, ItemsLayoutOrientation itemsLayoutOrientation, bool goingNext) { int firstVisibleItemIndex = -1; int lastVisibleItemIndex = -1; var itemsPanel = (listViewBase.ItemsPanelRoot as ItemsStackPanel); if (itemsPanel != null) { firstVisibleItemIndex = itemsPanel.FirstVisibleIndex; lastVisibleItemIndex = itemsPanel.LastVisibleIndex; } else { var scrollViewer = listViewBase.GetFirstDescendant <ScrollViewer>(); var presenters = listViewBase.GetChildren <ListViewItemPresenter>(); if (presenters != null || scrollViewer == null) { int count = 0; foreach (ListViewItemPresenter presenter in presenters) { if (IsListViewItemVisible(presenter, scrollViewer, itemsLayoutOrientation)) { if (firstVisibleItemIndex == -1) { firstVisibleItemIndex = count; } lastVisibleItemIndex = count; } count++; } } } double center = (lastVisibleItemIndex + firstVisibleItemIndex) / 2.0; int centerItemIndex = goingNext ? (int)Math.Ceiling(center) : (int)Math.Floor(center); return(firstVisibleItemIndex, lastVisibleItemIndex, centerItemIndex); }