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 the ListView scroll position center and end without animation #27001

Open
wants to merge 3 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 @@ -656,26 +656,25 @@ async void ScrollTo(object group, object item, ScrollToPosition toPosition, bool
else
semanticLocation.Bounds = new WRect(0, viewportHeight - tHeight, 0, 0);

// Waiting for loaded doesn't seem to be enough anymore; the ScrollViewer does not appear until after Loaded.
// Even if the ScrollViewer is present, an invoke at low priority fails (E_FAIL) presumably because the items are
// still loading. An invoke at idle sometimes work, but isn't reliable enough, so we'll just have to commit
// treason and use a blanket catch for the E_FAIL and try again.
try
{
List.MakeVisible(semanticLocation);
}
catch (Exception)
{
if (previouslyFailed)
return;

Task.Delay(1).ContinueWith(ct => { ScrollTo(group, item, toPosition, shouldAnimate, includeGroup, true); }, TaskScheduler.FromCurrentSynchronizationContext()).WatchForError();
}
break;
}
}
});

// Waiting for loaded doesn't seem to be enough anymore; the ScrollViewer does not appear until after Loaded.
// Even if the ScrollViewer is present, an invoke at low priority fails (E_FAIL) presumably because the items are
// still loading. An invoke at idle sometimes work, but isn't reliable enough, so we'll just have to commit
// treason and use a blanket catch for the E_FAIL and try again.
try
{
List.MakeVisible(semanticLocation);
}
catch (Exception)
{
if (previouslyFailed)
return;

Task.Delay(1).ContinueWith(ct => { ScrollTo(group, item, toPosition, shouldAnimate, includeGroup, true); }, TaskScheduler.FromCurrentSynchronizationContext()).WatchForError();
}
}

void OnElementScrollToRequested(object sender, ScrollToRequestedEventArgs e)
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/Issue26945.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, 26945, "ListView ScrollTo position always remains at the start even when set to Center or End without animation", PlatformAffected.All)]
public class Issue26945 : ContentPage
{
private ListView ListView;
private ObservableCollection<string> Items;

public Issue26945()
{
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, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could modify the sample using for example, a CheckBox, to use the ScrollTo method with the animated parameter using true and also false values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsuarezruiz, The current PR includes a test case without animation. We already have a test case with animation in this PR. Could you please review it and share your feedback?

break;
case ScrollToPosition.Center:
ListView.ScrollTo(ListView.SelectedItem, ScrollToPosition.Center, false);
break;
case ScrollToPosition.End:
ListView.ScrollTo(ListView.SelectedItem, ScrollToPosition.End, false);
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using NUnit.Framework;
using NUnit.Framework.Legacy;
using UITest.Appium;
using UITest.Core;

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

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

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

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

[Test]
[Category(UITestCategories.ListView)]
public void Issue26945Test_SelectItemPositionEnd()
{
App.WaitForElement("EndButton");
App.Tap("EndButton");
VerifyScreenshot();
}
}
}
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