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

[Android] Fixed removing items from grouped list #26082

Open
wants to merge 1 commit 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 @@ -329,6 +329,16 @@ public override AView GetView(int position, AView convertView, ViewGroup parent)
return layout;
}

bool isHeader = cell.GetIsGroupHeader<ItemsView<Cell>, Cell>();

if (isHeader)
{
// We need to re-render the header to ensure it is properly measured and laid out.
// This is necessary because headers often have different layouts and sizes compared to regular cells,
// and reusing a view for a header can result in incorrect measurements and layout issues.
convertView = null;
}

AView view = CellFactory.GetCell(cell, convertView, parent, _context, _listView);

Performance.Start(reference, "AddView");
Expand All @@ -337,6 +347,7 @@ public override AView GetView(int position, AView convertView, ViewGroup parent)
{
if (convertView != view)
{
view.RemoveFromParent();
layout.RemoveViewAt(0);
layout.AddView(view, 0);
}
Expand All @@ -351,8 +362,6 @@ public override AView GetView(int position, AView convertView, ViewGroup parent)

Performance.Stop(reference, "AddView");

bool isHeader = cell.GetIsGroupHeader<ItemsView<Cell>, Cell>();

AView bline;

bool isSeparatorVisible = _listView.SeparatorVisibility == SeparatorVisibility.Default;
Expand Down
58 changes: 58 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue26015.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns:controls="clr-namespace:Maui.Controls.Sample.Issues"
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.Issue26015">
<RefreshView>
<ScrollView>
<Grid RowDefinitions="*,*">
<ListView Grid.Row="0"
ItemsSource="{Binding Data}"
IsGroupingEnabled="True"
HasUnevenRows="True">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Name}"/>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label Text="{Binding Detail}"/>
<Button Text="Remove"
AutomationId="{Binding Detail}"
Command="{Binding Source={RelativeSource AncestorType={x:Type local:Issue26015ViewModel}}, Path=RemoveItemCommand}"
CommandParameter="{Binding .}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView
Grid.Row="1"
ItemsSource="{Binding Data}"
IsGroupingEnabled="True"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label Text="{Binding Detail}"/>
<Button
Text="Remove"
Command="{Binding Source={RelativeSource AncestorType={x:Type local:Issue26015ViewModel}}, Path=RemoveItemCommand}"
CommandParameter="{Binding .}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</ScrollView>
</RefreshView>
</ContentPage>
77 changes: 77 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue26015.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Maui.Controls.Sample.Issues
{

[Issue(IssueTracker.Github, 26015, "MAUI Grouped ListView Remove Item Causes Error when more than one Group item exists", PlatformAffected.Android)]
public partial class Issue26015 : ContentPage
{
public Issue26015()
{
InitializeComponent();
BindingContext = new Issue26015ViewModel();
}
}

class Issue26015ViewModel : ViewModelBase
{
public class GroupData : ObservableCollection<DetailData>
{
public string Name { get; private set; }
public GroupData(string name, ObservableCollection<DetailData> d) : base(d)
{ Name = name; }
}

public class DetailData
{
public int Nr { get; set; }
public string Detail { get; set; }
}

private ObservableCollection<GroupData> _data;
public ObservableCollection<GroupData> Data
{
get => _data;
set
{
_data = value;
OnPropertyChanged();
}
}

public Command RemoveItemCommand => new Command<DetailData>(RemoveItem);

public Issue26015ViewModel()
{
Data =
[
new GroupData("Group1",
[
new(){ Nr = 1, Detail = "Test1" },
new(){ Nr = 2, Detail = "Test2" }
]),
new GroupData("Group2",
[
new(){ Nr = 3, Detail = "Test3" },
new(){ Nr = 4, Detail = "Test4" }
]),
];
}

private void RemoveItem(DetailData data)
{
foreach (var item in Data)
{
foreach (var child in item)
{
if (child.Nr == data.Nr)
{
item.Remove(child);
break;
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue26015 : _IssuesUITest
{

public Issue26015(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "MAUI Grouped ListView Remove Item Causes Error when more than one Group item exists";

[Test]
[Category(UITestCategories.ListView)]
public void AppShouldNotCrashWhenRemovingItemsFromGroupedList()
{
App.WaitForElement("Test1");
App.Click("Test1");
App.Click("Test2");
App.Click("Test3");
App.Click("Test4");
}
}
Loading