From 22dd496206f6d96654d7b0fecfb74a5100334591 Mon Sep 17 00:00:00 2001 From: Kevin Petit Date: Thu, 20 Jun 2019 04:27:25 +0200 Subject: [PATCH] [Android] Dispose the collection view item adapter (#6525) * [Android] Dispose the collection view adapter. * More complete fix. --- .../CollectionView/ItemsViewRenderer.cs | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/Xamarin.Forms.Platform.Android/CollectionView/ItemsViewRenderer.cs b/Xamarin.Forms.Platform.Android/CollectionView/ItemsViewRenderer.cs index 84d0c6e7a..1656c1d76 100644 --- a/Xamarin.Forms.Platform.Android/CollectionView/ItemsViewRenderer.cs +++ b/Xamarin.Forms.Platform.Android/CollectionView/ItemsViewRenderer.cs @@ -19,7 +19,7 @@ namespace Xamarin.Forms.Platform.Android readonly EffectControlProvider _effectControlProvider; protected ItemsViewAdapter ItemsViewAdapter; - + int? _defaultLabelFor; bool _disposed; @@ -139,7 +139,10 @@ namespace Xamarin.Forms.Platform.Android if (Element != null) { TearDownOldElement(Element as ItemsView); + } + if (Element != null) + { if (Platform.GetRenderer(Element) == this) { Element.ClearValue(Platform.RendererProperty); @@ -215,9 +218,6 @@ namespace Xamarin.Forms.Platform.Android return; } - // Stop watching the old adapter to see if it's empty (if we _are_ watching) - Unwatch(ItemsViewAdapter ?? GetAdapter()); - UpdateAdapter(); UpdateEmptyView(); @@ -225,8 +225,16 @@ namespace Xamarin.Forms.Platform.Android protected virtual void UpdateAdapter() { + var oldItemViewAdapter = ItemsViewAdapter; + + // Stop watching the old adapter to see if it's empty (if we are watching) + Unwatch(oldItemViewAdapter); + ItemsViewAdapter = new ItemsViewAdapter(ItemsView); + SwapAdapter(ItemsViewAdapter, true); + + oldItemViewAdapter?.Dispose(); } void Unwatch(Adapter adapter) @@ -281,7 +289,7 @@ namespace Xamarin.Forms.Platform.Android _layout = ItemsView.ItemsLayout; SetLayoutManager(SelectLayoutManager(_layout)); - + UpdateSnapBehavior(); UpdateBackgroundColor(); UpdateFlowDirection(); @@ -296,7 +304,7 @@ namespace Xamarin.Forms.Platform.Android // Listen for ScrollTo requests ItemsView.ScrollToRequested += ScrollToRequested; } - + protected virtual void TearDownOldElement(ItemsView oldElement) { if (oldElement == null) @@ -316,12 +324,13 @@ namespace Xamarin.Forms.Platform.Android // Stop listening for ScrollTo requests oldElement.ScrollToRequested -= ScrollToRequested; - var adapter = GetAdapter(); - - if (adapter != null) + if (ItemsViewAdapter != null) { + Unwatch(ItemsViewAdapter); + SetAdapter(null); - adapter.Dispose(); + + ItemsViewAdapter.Dispose(); } if (_snapManager != null) @@ -349,7 +358,7 @@ namespace Xamarin.Forms.Platform.Android { UpdateSnapBehavior(); } - else if (propertyChanged.IsOneOf(ListItemsLayout.ItemSpacingProperty, + else if (propertyChanged.IsOneOf(ListItemsLayout.ItemSpacingProperty, GridItemsLayout.HorizontalItemSpacingProperty, GridItemsLayout.VerticalItemSpacingProperty)) { UpdateItemSpacing(); @@ -408,6 +417,7 @@ namespace Xamarin.Forms.Platform.Android _emptyViewAdapter.EmptyView = emptyView; _emptyViewAdapter.EmptyViewTemplate = emptyViewTemplate; + Watch(ItemsViewAdapter); } else @@ -479,7 +489,7 @@ namespace Xamarin.Forms.Platform.Android protected virtual void ScrollTo(ScrollToRequestEventArgs args) { var position = DeterminePosition(args); - + if (args.IsAnimated) { ScrollHelper.AnimateScrollToPosition(position, args.ScrollToPosition);