Skip to content

Commit

Permalink
Fix crash removing items in ListView (#7411)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz committed May 23, 2022
1 parent ba97726 commit 4b64fd9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Layout/LayoutHandler.Android.cs
Expand Up @@ -40,7 +40,7 @@ public override void SetVirtualView(IView view)

foreach (var child in VirtualView.OrderByZIndex())
{
PlatformView.AddView(child.ToPlatform(MauiContext));
PlatformView.SafelyAddView(child.ToPlatform(MauiContext));
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/Core/src/Platform/Android/ViewGroupExtensions.cs
Expand Up @@ -6,6 +6,14 @@ namespace Microsoft.Maui.Platform
{
public static class ViewGroupExtensions
{
public static void SafelyAddView(this AViewGroup viewGroup, AView child)
{
if (child.Parent != null)
child.RemoveFromParent();

viewGroup.AddView(child);
}

public static IEnumerable<T> GetChildrenOfType<T>(this AViewGroup viewGroup) where T : AView
{
for (var i = 0; i < viewGroup.ChildCount; i++)
Expand Down

0 comments on commit 4b64fd9

Please sign in to comment.