Skip to content
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 @@ -146,7 +146,8 @@ protected virtual void ScrollToRequested(object sender, ScrollToRequestEventArgs
return;
}

var position = Items.ScrollToPositionExtensions.ToCollectionViewScrollPosition(args.ScrollToPosition, UICollectionViewScrollDirection.Vertical);
var scrollDirection = Controller.GetScrollDirection();
var position = Items.ScrollToPositionExtensions.ToCollectionViewScrollPosition(args.ScrollToPosition, scrollDirection);

Controller.CollectionView.ScrollToItem(indexPath,
position, args.IsAnimated);
Expand Down
85 changes: 85 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue33852.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 33852, "CollectionView ScrollTo does not work with horizontal layout", PlatformAffected.iOS | PlatformAffected.macOS)]
public class Issue33852 : ContentPage
{
public ObservableCollection<string> Items { get; set; }
private Label _indexLabel;
private CollectionView2 _collectionView;
public Issue33852()
{
Items = new ObservableCollection<string>();
for (int i = 0; i <= 50; i++)
{
Items.Add($"Item_{i}");
}

_indexLabel = new Label
{
AutomationId = "IndexLabel",
Text = "ItemIndex: 0"
};

var scrollToButton = new Button
{
AutomationId = "ScrollToButton",
Text = "Scroll to Item 15",
WidthRequest = 150
};
scrollToButton.Clicked += OnScrollToButtonClicked;

_collectionView = new CollectionView2
{
AutomationId = "TestCollectionView",
ItemsSource = Items,
HeightRequest = 600,
ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal)
{
ItemSpacing = 10
},
ItemTemplate = new DataTemplate(() =>
{
var label = new Label();
label.SetBinding(Label.TextProperty, ".");
return new Border
{
Margin = new Thickness(5),
Padding = new Thickness(10),
Stroke = Colors.Gray,
Content = label
};
})
};

_collectionView.Scrolled += OnCollectionViewScrolled;

Content = new StackLayout
{
Children =
{
_indexLabel,
scrollToButton,
_collectionView
}
};
}

private void OnCollectionViewScrolled(object sender, ItemsViewScrolledEventArgs e)
{
if (e.FirstVisibleItemIndex > 0)
{
_indexLabel.Text = $"The CollectionView is scrolled";
}
else
{
_indexLabel.Text = "The CollectionView does not scroll";
}
}

private void OnScrollToButtonClicked(object sender, EventArgs e)
{
_collectionView.ScrollTo(15, position: ScrollToPosition.Start, animate: true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue33852 : _IssuesUITest
{
public Issue33852(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "CollectionView ScrollTo does not work with horizontal layout";

[Test]
[Category(UITestCategories.CollectionView)]
public void ProgrammaticScrollToWorksWithHorizontalLayout()
{
App.WaitForElement("ScrollToButton");
App.Tap("ScrollToButton");
var firstIndexText = App.FindElement("IndexLabel").GetText();
Assert.That(firstIndexText, Is.EqualTo("The CollectionView is scrolled"));
}
}
Loading