Skip to content
Merged
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 @@ -12,6 +12,7 @@ public class RecyclerViewScrollListener<TItemsView, TItemsViewSource> : Recycler
int _horizontalOffset, _verticalOffset;
TItemsView _itemsView;
readonly bool _getCenteredItemOnXAndY = false;
bool _hasCompletedFirstLayout = false;

public RecyclerViewScrollListener(TItemsView itemsView, ItemsViewAdapter<TItemsView, TItemsViewSource> itemsViewAdapter) : this(itemsView, itemsViewAdapter, false)
{
Expand All @@ -28,6 +29,8 @@ public RecyclerViewScrollListener(TItemsView itemsView, ItemsViewAdapter<TItemsV
internal void UpdateAdapter(ItemsViewAdapter<TItemsView, TItemsViewSource> itemsViewAdapter)
{
ItemsViewAdapter = itemsViewAdapter;
// Reset flag when adapter changes to handle ItemsSource updates
_hasCompletedFirstLayout = false;
}

public override void OnScrolled(RecyclerView recyclerView, int dx, int dy)
Expand All @@ -41,6 +44,17 @@ public override void OnScrolled(RecyclerView recyclerView, int dx, int dy)
_horizontalOffset += dx;
_verticalOffset += dy;

// Prevent the Scrolled event from firing on the very first layout callback only.
// This is the initial OnScrolled(0,0) call when the view is first laid out.
// After that, layout is marked as complete and all subsequent scroll events are allowed.
if (!_hasCompletedFirstLayout && !recyclerView.IsLaidOut && dx == 0 && dy == 0)
{
return;
}

// Mark that first layout has been processed - all future scrolls should fire events
_hasCompletedFirstLayout = true;

var (First, Center, Last) = GetVisibleItemsIndex(recyclerView);
var itemsViewScrolledEventArgs = new ItemsViewScrolledEventArgs
{
Expand Down
65 changes: 65 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue33333.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Collections.ObjectModel;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 33333, "CollectionView Scrolled event is triggered on the initial app load", PlatformAffected.Android)]
public class Issue33333 : ContentPage
{
int _scrolledEventCount = 0;

public Issue33333()
{
var items = new ObservableCollection<string>();
for (int i = 0; i < 50; i++)
{
items.Add($"Item {i}");
}

var scrollCountLabel = new Label
{
AutomationId = "ScrollCountLabel",
Text = "Scrolled Event Count: 0",
FontSize = 18,
Padding = new Thickness(10),
BackgroundColor = Colors.LightYellow
};

var collectionView = new CollectionView
{
AutomationId = "TestCollectionView",
ItemsSource = items,
ItemTemplate = new DataTemplate(() =>
{
var label = new Label
{
Padding = new Thickness(10),
VerticalOptions = LayoutOptions.Center
};
label.SetBinding(Label.TextProperty, ".");
return label;
})
};

collectionView.Scrolled += (sender, e) =>
{
_scrolledEventCount++;
scrollCountLabel.Text = $"Scrolled Event Count: {_scrolledEventCount}";
};

var instructionLabel = new Label
{
AutomationId = "InstructionLabel",
Text = "The Scrolled event count should remain 0 on initial load. On Android, it incorrectly fires immediately.",
Padding = new Thickness(10),
FontSize = 14,
TextColor = Colors.Gray
};

Content = new StackLayout
{
Children = { scrollCountLabel, instructionLabel, collectionView }
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue33333 : _IssuesUITest
{
public override string Issue => "CollectionView Scrolled event is triggered on the initial app load";

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

[Test]
[Category(UITestCategories.CollectionView)]
public void CollectionViewScrolledEventShouldNotFireOnInitialLoad()
{
App.WaitForElement("ScrollCountLabel");

var scrollCountText = App.FindElement("ScrollCountLabel").GetText();
Assert.That(scrollCountText, Is.EqualTo("Scrolled Event Count: 0"),
"Scrolled event should not fire on initial load without user interaction");
}
Comment on lines +15 to +22
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

The test only verifies that the Scrolled event doesn't fire on initial load, but doesn't validate that subsequent scrolling events work correctly after the initial load. Consider adding a second test case or extending this test to perform an actual scroll and verify that the event fires properly (e.g., scroll count should be 1 after user scrolls).

Copilot uses AI. Check for mistakes.
}
Loading