Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ListView.ScrollTo to Align SelectedItem with Center or End on Windows #26922

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,31 @@ async void OnViewChangeCompleted(object sender, SemanticZoomViewChangedEventArgs
ScrollTo(listProxy.ProxiedEnumerable, listProxy[0], ScrollToPosition.Start, true, true);
}

bool ScrollToItemWithAnimation(ScrollViewer viewer, object item)
bool ScrollToItemWithAnimation(ScrollViewer viewer, object item, ScrollToPosition toPosition)
{
var selectorItem = List.ContainerFromItem(item) as Microsoft.UI.Xaml.Controls.Primitives.SelectorItem;
var transform = selectorItem?.TransformToVisual(viewer.Content as UIElement);
var position = transform?.TransformPoint(new global::Windows.Foundation.Point(0, 0));
if (!position.HasValue)
return false;
// scroll with animation
viewer.ChangeView(position.Value.X, position.Value.Y, null);
switch (toPosition)
{
case ScrollToPosition.Center:
case ScrollToPosition.End:
{
double offset = viewer.ViewportHeight - selectorItem.DesiredSize.Height;
if (toPosition == ScrollToPosition.Center)
viewer.ChangeView(position.Value.X, position.Value.Y - (offset / 2), null);
else
viewer.ChangeView(position.Value.X, position.Value.Y - offset, null);
break;
}
default:
viewer.ChangeView(position.Value.X, position.Value.Y, null);
break;
}

return true;
}

Expand Down Expand Up @@ -617,7 +633,7 @@ async void ScrollTo(object group, object item, ScrollToPosition toPosition, bool
object c = t[location.Item2];

// scroll to desired item with animation
if (shouldAnimate && ScrollToItemWithAnimation(viewer, c))
if (shouldAnimate && ScrollToItemWithAnimation(viewer, c, toPosition))
return;

double viewportHeight = viewer.ViewportHeight;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
135 changes: 135 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue26857.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 26857, "ListView ScrollTo position always remains at the start even when set to Center or End", PlatformAffected.All)]
public class Issue26857 : ContentPage
{
private ListView ListView;
private ObservableCollection<string> Items;

public Issue26857()
{
Items = new ObservableCollection<string>
{
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5",
"Item 6",
"Item 7",
"Item 8",
"Item 9",
"Item 10",
"Item 11",
"Item 12",
"Item 13",
"Item 14",
"Item 15",
"Item 16",
"Item 17",
"Item 18",
"Item 19",
"Item 20",
"Item 21",
"Item 22",
"Item 23",
"Item 24",
"Item 25",
};

ListView = new ListView
{
ItemsSource = Items,
SelectedItem = "Item 10",
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
WidthRequest = 300,
HeightRequest = 220,
};

var horizontalStack = new StackLayout
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.Center,
Spacing = 20
};

var startButton = new Button
{
Text = "Start",
AutomationId = "StartButton",
};
startButton.Clicked += ScrollPositionStart_Clicked;

var centerButton = new Button
{
Text = "Center",
AutomationId = "CenterButton",
};
centerButton.Clicked += ScrollPositionCenter_Clicked;

var endButton = new Button
{
Text = "End",
AutomationId = "EndButton",
};
endButton.Clicked += ScrollPositionEnd_Clicked;

horizontalStack.Children.Add(startButton);
horizontalStack.Children.Add(centerButton);
horizontalStack.Children.Add(endButton);

var stackLayout = new StackLayout
{
Orientation = StackOrientation.Vertical,
Spacing = 20
};

stackLayout.Children.Add(ListView);
stackLayout.Children.Add(horizontalStack);
this.Content = stackLayout;
}

private void ScrollToSelectedItem(ScrollToPosition toPosition)
{
if (ListView?.SelectedItem != null)
{
switch (toPosition)
{
case ScrollToPosition.Start:
ListView.ScrollTo(ListView.SelectedItem, ScrollToPosition.Start, true);
break;
case ScrollToPosition.Center:
ListView.ScrollTo(ListView.SelectedItem, ScrollToPosition.Center, true);
break;
case ScrollToPosition.End:
ListView.ScrollTo(ListView.SelectedItem, ScrollToPosition.End, true);
break;
}
}
}

private void ScrollPositionStart_Clicked(object sender, EventArgs e)
{
var currentIndex = Items.IndexOf(ListView.SelectedItem as string);
ListView.SelectedItem = Items[currentIndex + 2];
ScrollToSelectedItem(ScrollToPosition.Start);
}

private void ScrollPositionCenter_Clicked(object sender, EventArgs e)
{
var currentIndex = Items.IndexOf(ListView.SelectedItem as string);
ListView.SelectedItem = Items[currentIndex + 2];
ScrollToSelectedItem(ScrollToPosition.Center);
}

private void ScrollPositionEnd_Clicked(object sender, EventArgs e)
{
var currentIndex = Items.IndexOf(ListView.SelectedItem as string);
ListView.SelectedItem = Items[currentIndex + 2];
ScrollToSelectedItem(ScrollToPosition.End);
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

using NUnit.Framework;
using NUnit.Framework.Legacy;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue26857 : _IssuesUITest
{
public override string Issue => "ListView ScrollTo position always remains at the start even when set to Center or End";

public Issue26857(TestDevice device) : base(device)
{
}

[Test]
[Category(UITestCategories.ListView)]
public void Issue26857_SelectItemPositionStart()
{
App.WaitForElement("StartButton");
App.Tap("StartButton");
VerifyScreenshot();
}

[Test]
[Category(UITestCategories.ListView)]
public void Issue26857Test_SelectItemPositionCenter()
{
App.WaitForElement("CenterButton");
App.Tap("CenterButton");
VerifyScreenshot();
}

[Test]
[Category(UITestCategories.ListView)]
public void Issue26857Test_SelectItemPositionEnd()
{
App.WaitForElement("EndButton");
App.Tap("EndButton");
VerifyScreenshot();
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading