xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] ListView with grouping and SwipeView throws IllegalStateException: The specified child already has a parent #13790

Closed thomasgalliker closed 2 years ago

thomasgalliker commented 3 years ago

Description

When a ListView is bound to a grouped collection and these groups are collapsed or expanded, the Android app crashes with an IllegalStateException (The specified child already has a parent. You must call removeView() on the child's parent first). This issue is probably cause by some kind of caching issue. It only happens if ListView uses CachingStrategy=RetainElement (default) and the ItemTemplate is using a SwipeView as content. The crash might happens somewhere around this line of code: https://github.com/xamarin/Xamarin.Forms/blob/50e6fbb754d1ed8efa26a17659c80474498769b2/Xamarin.Forms.Platform.Android/Renderers/SwipeViewRenderer.cs#L367

Steps to Reproduce

  1. Create an app which bind a collection of grouped items to a ListView
  2. Set IsGroupingEnabled=True on the ListView
  3. Set a GroupHeaderTemplate as well as an ItemTemplate on the ListView
  4. Use a SwipeView inside the ItemTemplate
  5. Add a TapGestureRecognizer in the GroupHeaderTemplate which is used to toggle collapse/expanded state of each group. See class GroupedIssueModel in the attached sample app.

Expected Behavior

When the ListView is configured with CachingStrategy=RecycleElement, everything works fine. As you may know, RecycleElement causes some unintended effects in certain scenarios; I think this sample app should work with RetainElement without crashing.

Actual Behavior

When the ListView is configured with CachingStrategy=RetainElement (which is the default), the app crashes when the grouping is collapsed. You can observe the exception in Java.Lang.IllegalStateException The specified child already has a parent. You must call removeView() on the child's parent first.

Basic Information

Environment

Show/Hide Visual Studio info ``` === Visual Studio Community 2019 for Mac === Version 8.8.9 (build 10) Installation UUID: 5382f3a5-0fef-4f4d-abb5-bc7041f1db53 GTK+ 2.24.23 (Raleigh theme) Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638) Package version: 612000122 === Mono Framework MDK === Runtime: Mono 6.12.0.122 (2020-02/c621c35ffa0) (64-bit) Package version: 612000122 === Roslyn (Language Service) === 3.8.0-5.20519.18+4c195c3ac1974edcefa76774d7a59a2350ec55fa === NuGet === Version: 5.8.0.6860 === .NET Core SDK === SDK: /usr/local/share/dotnet/sdk/5.0.103/Sdks SDK Versions: 5.0.103 5.0.102 5.0.101 3.1.406 3.1.405 3.1.404 3.1.403 3.1.402 3.1.401 3.1.302 3.1.301 3.1.300 3.1.202 3.1.200 3.1.102 3.1.101 3.1.100 3.0.102 3.0.100 2.2.207 MSBuild SDKs: /Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/Sdks === .NET Core Runtime === Runtime: /usr/local/share/dotnet/dotnet Runtime Versions: 5.0.3 5.0.2 5.0.1 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.7 3.1.6 3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.2 3.0.0 2.2.8 2.1.23 2.1.22 2.1.21 2.1.20 2.1.19 2.1.18 2.1.17 2.1.16 2.1.15 2.1.14 === .NET Core 3.1 SDK === SDK: 3.1.406 === Xamarin.Profiler === Version: 1.6.13.11 Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler === Updater === Version: 11 === Apple Developer Tools === Xcode 12.4 (17801) Build 12D4e === Xamarin.Mac === Version: 7.4.0.4 (Visual Studio Community) Hash: 5a05865f6 Branch: xcode12.4 Build date: 2021-01-28 02:30:22-0500 === Xamarin.iOS === Version: 14.10.0.4 (Visual Studio Community) Hash: 5a05865f6 Branch: xcode12.4 Build date: 2021-01-28 02:30:23-0500 === Xamarin Designer === Version: 16.8.0.510 Hash: 44e3f3ce9 Branch: remotes/origin/d16-8 Build date: 2020-12-10 00:06:14 UTC === Xamarin.Android === Version: 11.1.0.26 (Visual Studio Community) Commit: xamarin-android/d16-8/a36ce73 Android SDK: /Users/exttga/Library/Developer/Xamarin/android-sdk-macosx Supported Android versions: 8.1 (API level 27) SDK Tools Version: 26.1.1 SDK Platform Tools Version: 30.0.5 SDK Build Tools Version: 29.0.2 Build Information: Mono: 5e9cb6d Java.Interop: xamarin/java.interop/d16-8@79d9533 ProGuard: Guardsquare/proguard/proguard6.2.2@ebe9000 SQLite: xamarin/sqlite/3.32.1@1a3276b Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-8@2fb1cbc === Microsoft OpenJDK for Mobile === Java SDK: /Users/exttga/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25 1.8.0-25 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL === Android SDK Manager === Version: 16.8.0.32 Hash: 01a7774 Branch: remotes/origin/d16-8 Build date: 2021-01-14 00:34:58 UTC === Android Device Manager === Version: 16.8.0.46 Hash: 0a81419 Branch: remotes/origin/d16-8 Build date: 2021-01-14 00:35:22 UTC === Build Information === Release ID: 808090010 Git revision: 0061f975f58a5dba53e339f116371f4853edeb75 Build date: 2021-02-11 11:16:18-05 Build branch: release-8.8 Xamarin extensions: 0061f975f58a5dba53e339f116371f4853edeb75 === Operating System === Mac OS X 10.15.7 Darwin 19.6.0 Darwin Kernel Version 19.6.0 Tue Jan 12 22:13:05 PST 2021 root:xnu-6153.141.16~1/RELEASE_X86_64 x86_64 ```

Screenshots

Screen Recording with ListView.CachingStrategy=RetainElement (crashes) https://user-images.githubusercontent.com/10376364/108231933-8d757f00-7142-11eb-81c6-b6b774770cf5.mp4

Screen Recording with ListView.CachingStrategy=RecycleElement (works) https://user-images.githubusercontent.com/10376364/108233296-ea256980-7143-11eb-9a89-150c91faab34.MOV

Reproduction Link

GroupingListSample.zip

Workaround

Welchen commented 3 years ago

Any update on this issue. Just started working on an Android head to my project and ran into this problem after already having it working on iOS.

Same issue of having a SwipeView in a ListView.

Welchen commented 3 years ago

@thomasgalliker / @hartez any update on this?

thomasgalliker commented 3 years ago

@Welchen, I think @samhouts is dispatching issues at Xamarin/Forms. I’m curious too.

Welchen commented 3 years ago

@samhouts do you happen to know the status of this issue? It is still broken and it seems no on is looking into it.

Welchen commented 3 years ago

@thomasgalliker to try and get around the problem so we could release I switch out the SwipeView for a Button so the user could delete on any of the items in the list. Getting the same error. Feels like the issue is broken with ListView entirely and not just SwipeView.

Really wish someone would respond with some information.

pldeschamps commented 3 years ago

This

Use ListView.CachingStrategy=RecycleElement.

solved my issue. Thank you @thomasgalliker

ThanhNg commented 3 years ago

After 1 and a half year, any updates on this? Many thanks. @hartez @samhouts

DanielGlick commented 2 years ago

@jsuarezruiz I see that you made good progress on this bug. Any update on when this will be in an update?