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 @@ -63,10 +63,14 @@ public bool OnItemMove(int fromPosition, int toPosition)
}
else if (itemsView.ItemsSource is IList list)
{
var fromItem = list[fromPosition];
var hasHeader = itemsSource.HasHeader;
var fromPositionInSource = hasHeader ? fromPosition - 1 : fromPosition;
var toPositionInSource = hasHeader ? toPosition - 1 : toPosition;

var fromItem = list[fromPositionInSource];
SetObserveChanges(itemsSource, false);
list.RemoveAt(fromPosition);
list.Insert(toPosition, fromItem);
list.RemoveAt(fromPositionInSource);
list.Insert(toPositionInSource, fromItem);
NotifyItemMoved(fromPosition, toPosition);
SetObserveChanges(itemsSource, true);
itemsView.SendReorderCompleted();
Expand Down
39 changes: 39 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue17823.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Maui.Controls.Sample.Issues"
x:Class="Maui.Controls.Sample.Issues.Issue17823"
Title="Issue 17823 - CollectionView reorder with header">
<VerticalStackLayout Padding="16" Spacing="12">
<CollectionView x:Name="ReorderCollectionView"
AutomationId="ReorderCollectionView"
ItemsSource="{Binding Items}"
CanReorderItems="True"
SelectionMode="None">
<CollectionView.Header>
<Grid Padding="12" BackgroundColor="#EEEEEE">
<Label Text="Header Content"
AutomationId="HeaderLabel"
FontAttributes="Bold"/>
</Grid>
</CollectionView.Header>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="local:Issue17823+ReorderItem">
<Grid Padding="16"
Margin="0,0,0,8"
BackgroundColor="#FFFFFF"
AutomationId="{Binding ContainerAutomationId}">
<Label Text="{Binding Text}"
FontSize="18"
HorizontalOptions="Center"
AutomationId="{Binding LabelAutomationId}"/>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

<Label x:Name="ReorderStatusLabel"
AutomationId="ReorderStatusLabel"
FontAttributes="Bold"/>
</VerticalStackLayout>
</ContentPage>
56 changes: 56 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue17823.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.ObjectModel;
using System.Linq;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 17823, "CollectionView reordering last item succeeds when header is present", PlatformAffected.Android)]
public partial class Issue17823 : ContentPage
{
public ObservableCollection<ReorderItem> Items { get; }

public Issue17823()
{
InitializeComponent();

Items = new ObservableCollection<ReorderItem>
{
new ReorderItem(0, "Item 1"),
new ReorderItem(1, "Item 2"),
new ReorderItem(2, "Item 3"),
new ReorderItem(3, "Item 4"),
};

BindingContext = this;

ReorderCollectionView.ReorderCompleted += OnReorderCompleted;

UpdateStatusLabel();
}

void OnReorderCompleted(object sender, EventArgs e)
{
UpdateStatusLabel();
}

void UpdateStatusLabel()
{
ReorderStatusLabel.Text = string.Join(", ", Items.Select(item => item.Text));
}

public class ReorderItem
{
public ReorderItem(int index, string text)
{
Text = text;
ContainerAutomationId = $"ReorderItem{index}";
LabelAutomationId = $"ReorderItemLabel{index}";
}

public string Text { get; set; }

public string ContainerAutomationId { get; }

public string LabelAutomationId { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue17823 : _IssuesUITest
{
public Issue17823(TestDevice device) : base(device)
{
}

public override string Issue => "CollectionView reordering last item succeeds when header is present";

[Test]
[Category(UITestCategories.CollectionView)]
public void ReorderingItemToEndWithHeaderDoesNotCrash()
{
// Verify header is present
App.WaitForElement("HeaderLabel");

// Verify all items are present
App.WaitForElement("ReorderItem0");
App.WaitForElement("ReorderItem3");

// Verify initial state
var initialText = App.FindElement("ReorderStatusLabel").GetText();
Assert.That(initialText, Is.EqualTo("Item 1, Item 2, Item 3, Item 4"));

// The bug: dragging first item to the end would crash with header present
// This is because adapter indices include header (0=header, 1=item0, 2=item1, etc.)
// but item source indices don't (0=item0, 1=item1, etc.)
App.DragAndDrop("ReorderItem0", "ReorderItem3");

// Verify reorder succeeded without crash by checking status label updated
var afterDrag = App.FindElement("ReorderStatusLabel").GetText();
Assert.That(afterDrag, Is.EqualTo("Item 2, Item 3, Item 4, Item 1"));
}

[Test]
[Category(UITestCategories.CollectionView)]
public void ReorderingLastItemWithHeaderDoesNotCrash()
{
// Verify header is present
App.WaitForElement("HeaderLabel");

// Verify items are present
App.WaitForElement("ReorderItem0");
App.WaitForElement("ReorderItem3");

// Verify initial state
var initialText = App.FindElement("ReorderStatusLabel").GetText();
Assert.That(initialText, Is.EqualTo("Item 1, Item 2, Item 3, Item 4"));

// Test dragging last item to first position
// This tests the opposite direction
App.DragAndDrop("ReorderItem3", "ReorderItem0");

// Verify reorder succeeded without crash
var afterDrag = App.FindElement("ReorderStatusLabel").GetText();
Assert.That(afterDrag, Is.EqualTo("Item 4, Item 1, Item 2, Item 3"));
}
}