From cd7837ec6d72e8fe645ab9e906ba44162c839c7b Mon Sep 17 00:00:00 2001
From: SuthiYuvaraj <92777079+SuthiYuvaraj@users.noreply.github.com>
Date: Mon, 13 Jan 2025 12:27:10 +0530
Subject: [PATCH 01/14] Fix for 26810
---
.../Handlers/Items/Android/MauiRecyclerView.cs | 11 ++---------
.../src/Core/Handlers/Items/iOS/ItemsViewLayout.cs | 14 ++++++++++++++
2 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs b/src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs
index ab43bb156c0a..4bae74e49e5c 100644
--- a/src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs
+++ b/src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs
@@ -374,15 +374,8 @@ protected virtual void UpdateItemsUpdatingScrollMode()
if (ItemsViewAdapter == null || ItemsView == null)
return;
- if (ItemsView.ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepItemsInView)
- {
- // Keeping the current items in view is the default, so we don't need to watch for data changes
- _itemsUpdateScrollObserver.Stop(ItemsViewAdapter);
- }
- else
- {
- _itemsUpdateScrollObserver.Start(ItemsViewAdapter);
- }
+ // We need to observe the adapter for changes for the ItemsUpdatingScrollMode.
+ _itemsUpdateScrollObserver.Start(ItemsViewAdapter);
}
public virtual void ScrollTo(ScrollToRequestEventArgs args)
diff --git a/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs b/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs
index c6f8473b629d..c57be4802432 100644
--- a/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs
+++ b/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs
@@ -500,6 +500,10 @@ public override void FinalizeCollectionViewUpdates()
{
ForceScrollToLastItem(CollectionView, _itemsLayout);
}
+ else if (ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepItemsInView)
+ {
+ ForceScrollToFirstItem(CollectionView, _itemsLayout);
+ }
}
void TrackOffsetAdjustment()
@@ -565,6 +569,16 @@ static bool UpdateWillShiftVisibleItems(UICollectionView collectionView, UIColle
return false;
}
+ //Fix Added for ItemsUpdatingScrollMode.KeepItemsInView
+ void ForceScrollToFirstItem(UICollectionView collectionView, ItemsLayout itemsLayout)
+ {
+ if (collectionView.NumberOfItemsInSection(0) > 0)
+ {
+ var indexPath = NSIndexPath.FromItemSection(0, 0);
+ collectionView.ScrollToItem(indexPath, UICollectionViewScrollPosition.Top, true);
+ }
+ }
+
static void ForceScrollToLastItem(UICollectionView collectionView, ItemsLayout itemsLayout)
{
var sections = (int)collectionView.NumberOfSections();
From ffd2647d558d513ddda4f415e8b799470e764d32 Mon Sep 17 00:00:00 2001
From: SuthiYuvaraj <92777079+SuthiYuvaraj@users.noreply.github.com>
Date: Mon, 13 Jan 2025 19:20:01 +0530
Subject: [PATCH 02/14] Update ItemsViewLayout.cs
---
.../src/Core/Handlers/Items/iOS/ItemsViewLayout.cs | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs b/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs
index c57be4802432..fd924541a02d 100644
--- a/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs
+++ b/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs
@@ -572,6 +572,13 @@ static bool UpdateWillShiftVisibleItems(UICollectionView collectionView, UIColle
//Fix Added for ItemsUpdatingScrollMode.KeepItemsInView
void ForceScrollToFirstItem(UICollectionView collectionView, ItemsLayout itemsLayout)
{
+ var sections = (int)collectionView.NumberOfSections();
+
+ if (sections == 0)
+ {
+ return;
+ }
+
if (collectionView.NumberOfItemsInSection(0) > 0)
{
var indexPath = NSIndexPath.FromItemSection(0, 0);
From df7a2e3f27c0804e49ca52f30a74ecd48c3d8e43 Mon Sep 17 00:00:00 2001
From: SuthiYuvaraj <92777079+SuthiYuvaraj@users.noreply.github.com>
Date: Wed, 15 Jan 2025 19:43:00 +0530
Subject: [PATCH 03/14] TestCases for 26810
---
.../TestCases.HostApp/Issues/Issue26810.xaml | 30 ++++++++++++
.../Issues/Issue26810.xaml.cs | 47 +++++++++++++++++++
.../Tests/Issues/Issue26810.cs | 29 ++++++++++++
3 files changed, 106 insertions(+)
create mode 100644 src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml
create mode 100644 src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml.cs
create mode 100644 src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue26810.cs
diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml b/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml
new file mode 100644
index 000000000000..753456a78d9a
--- /dev/null
+++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml.cs
new file mode 100644
index 000000000000..dbe800ea3e40
--- /dev/null
+++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml.cs
@@ -0,0 +1,47 @@
+using System.Collections.ObjectModel;
+
+namespace Maui.Controls.Sample.Issues;
+[Issue(IssueTracker.Github, 26810, "Scroll To first item in CollectionView when updating the collection with KeepItemsInView",
+ PlatformAffected.Android)]
+public partial class Issue26810 : ContentPage
+{
+ private ObservableCollection- Items { get; set; } = new ObservableCollection
- ();
+ private Random _random = new Random();
+
+ public Issue26810()
+ {
+ InitializeComponent();
+ for (int i = 1; i <= 10; i++)
+ {
+ Items.Add(new Item { Name = $"Preloaded Item {i}", AutomationId = $"Item{i}" });
+ }
+ CollectionView.ItemsSource = Items;
+ this.BindingContext = this;
+ }
+
+ private async void CollectionView_Loaded(object sender, EventArgs e)
+ {
+ // Adding items with a small delay in LoadedEvent
+ for (int i = 10; i <= 30; i++)
+ {
+ Items.Add(new Item { Name = $" Item {i}", AutomationId = $"Item{i}" });
+ await Task.Delay(500); // Simulate delay
+ }
+ }
+
+ private void ScrollToButton_Clicked(object sender, EventArgs e)
+ {
+ if (Items.Count > 0)
+ {
+ int randomIndex = _random.Next(Items.Count);
+ CollectionView.ScrollTo(randomIndex);
+ }
+ }
+}
+
+public class Item
+{
+ public string Name { get; set; }
+ public string AutomationId { get; set; }
+}
+
diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue26810.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue26810.cs
new file mode 100644
index 000000000000..d05699a7e611
--- /dev/null
+++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue26810.cs
@@ -0,0 +1,29 @@
+using NUnit.Framework;
+using NUnit.Framework.Legacy;
+using UITest.Appium;
+using UITest.Core;
+
+namespace Microsoft.Maui.TestCases.Tests.Issues
+{
+ internal class Issue26810 : _IssuesUITest
+ {
+ public override string Issue => "Scroll To first item in CollectionView when updating the collection with KeepItemsInView";
+
+ public Issue26810(TestDevice device) : base(device)
+ {
+ }
+
+ [Test]
+ [Category(UITestCategories.ActivityIndicator)]
+ public void ScrollToFirstItemOnCollectionChanged()
+ {
+ // Is a Android and iOS issue; see https://github.com/dotnet/maui/issues/26810
+ App.WaitForElement("26810MainGrid");
+ App.WaitForElement("Item1");
+ App.WaitForElement("26810Button");
+ App.Click("26810Button");
+ App.WaitForElement("Item1");
+
+ }
+ }
+}
From 03c9f3bae7dfed0be528c4a535e986785f238ec6 Mon Sep 17 00:00:00 2001
From: SuthiYuvaraj <92777079+SuthiYuvaraj@users.noreply.github.com>
Date: Wed, 15 Jan 2025 20:11:32 +0530
Subject: [PATCH 04/14] Update Issue26810.xaml
---
src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml b/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml
index 753456a78d9a..ea323aac4049 100644
--- a/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml
+++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml
@@ -2,7 +2,7 @@
-
From 085d9242a2052e113b0bcd26086cdc84ac662ac4 Mon Sep 17 00:00:00 2001
From: SuthiYuvaraj <92777079+SuthiYuvaraj@users.noreply.github.com>
Date: Wed, 15 Jan 2025 20:59:09 +0530
Subject: [PATCH 05/14] Update Issue26810.xaml.cs
---
.../tests/TestCases.HostApp/Issues/Issue26810.xaml.cs | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml.cs
index dbe800ea3e40..744957275e29 100644
--- a/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml.cs
+++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml.cs
@@ -5,7 +5,7 @@ namespace Maui.Controls.Sample.Issues;
PlatformAffected.Android)]
public partial class Issue26810 : ContentPage
{
- private ObservableCollection
- Items { get; set; } = new ObservableCollection
- ();
+ private ObservableCollection Items { get; set; } = new ObservableCollection();
private Random _random = new Random();
public Issue26810()
@@ -13,7 +13,7 @@ public Issue26810()
InitializeComponent();
for (int i = 1; i <= 10; i++)
{
- Items.Add(new Item { Name = $"Preloaded Item {i}", AutomationId = $"Item{i}" });
+ Items.Add(new Issue26810ItemModel { Name = $"Preloaded Item {i}", AutomationId = $"Item{i}" });
}
CollectionView.ItemsSource = Items;
this.BindingContext = this;
@@ -24,7 +24,7 @@ private async void CollectionView_Loaded(object sender, EventArgs e)
// Adding items with a small delay in LoadedEvent
for (int i = 10; i <= 30; i++)
{
- Items.Add(new Item { Name = $" Item {i}", AutomationId = $"Item{i}" });
+ Items.Add(new Issue26810ItemModel { Name = $" Item {i}", AutomationId = $"Item{i}" });
await Task.Delay(500); // Simulate delay
}
}
@@ -39,9 +39,8 @@ private void ScrollToButton_Clicked(object sender, EventArgs e)
}
}
-public class Item
+public class Issue26810ItemModel
{
public string Name { get; set; }
public string AutomationId { get; set; }
}
-
From 1e9fcb9aab5c4b71a3b9992a4cef4160491a38ec Mon Sep 17 00:00:00 2001
From: SuthiYuvaraj <92777079+SuthiYuvaraj@users.noreply.github.com>
Date: Wed, 15 Jan 2025 20:51:48 +0530
Subject: [PATCH 06/14] Update Issue26810.cs
---
.../tests/TestCases.Shared.Tests/Tests/Issues/Issue26810.cs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue26810.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue26810.cs
index d05699a7e611..6edd8a95be63 100644
--- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue26810.cs
+++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue26810.cs
@@ -14,7 +14,7 @@ public Issue26810(TestDevice device) : base(device)
}
[Test]
- [Category(UITestCategories.ActivityIndicator)]
+ [Category(UITestCategories.CollectionView)]
public void ScrollToFirstItemOnCollectionChanged()
{
// Is a Android and iOS issue; see https://github.com/dotnet/maui/issues/26810
From de4acfe9de173bbf19995dacd3219dd1ae9afe01 Mon Sep 17 00:00:00 2001
From: SuthiYuvaraj <92777079+SuthiYuvaraj@users.noreply.github.com>
Date: Wed, 15 Jan 2025 22:36:40 +0530
Subject: [PATCH 07/14] Update Issue26810.xaml
---
src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml b/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml
index ea323aac4049..a2d3bc260339 100644
--- a/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml
+++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue26810.xaml
@@ -2,7 +2,7 @@
-