Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

[Android] Dispose the collection view item adapter #6525

Merged
merged 2 commits into from
Jun 20, 2019
Merged
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
34 changes: 22 additions & 12 deletions Xamarin.Forms.Platform.Android/CollectionView/ItemsViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ItemsViewRenderer : RecyclerView, IVisualElementRenderer, IEffectCo
readonly EffectControlProvider _effectControlProvider;

protected ItemsViewAdapter ItemsViewAdapter;

int? _defaultLabelFor;
bool _disposed;

Expand Down Expand Up @@ -139,7 +139,10 @@ protected override void Dispose(bool disposing)
if (Element != null)
{
TearDownOldElement(Element as ItemsView);
}

if (Element != null)
{
if (Platform.GetRenderer(Element) == this)
{
Element.ClearValue(Platform.RendererProperty);
Expand Down Expand Up @@ -215,18 +218,23 @@ protected virtual void UpdateItemsSource()
return;
}

// Stop watching the old adapter to see if it's empty (if we _are_ watching)
Unwatch(ItemsViewAdapter ?? GetAdapter());

UpdateAdapter();

UpdateEmptyView();
}

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)
Expand Down Expand Up @@ -281,7 +289,7 @@ protected virtual void SetUpNewElement(ItemsView newElement)

_layout = ItemsView.ItemsLayout;
SetLayoutManager(SelectLayoutManager(_layout));

UpdateSnapBehavior();
UpdateBackgroundColor();
UpdateFlowDirection();
Expand All @@ -296,7 +304,7 @@ protected virtual void SetUpNewElement(ItemsView newElement)
// Listen for ScrollTo requests
ItemsView.ScrollToRequested += ScrollToRequested;
}

protected virtual void TearDownOldElement(ItemsView oldElement)
{
if (oldElement == null)
Expand All @@ -316,12 +324,13 @@ protected virtual void TearDownOldElement(ItemsView oldElement)
// 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)
Expand Down Expand Up @@ -349,7 +358,7 @@ protected virtual void LayoutOnPropertyChanged(object sender, PropertyChangedEve
{
UpdateSnapBehavior();
}
else if (propertyChanged.IsOneOf(ListItemsLayout.ItemSpacingProperty,
else if (propertyChanged.IsOneOf(ListItemsLayout.ItemSpacingProperty,
GridItemsLayout.HorizontalItemSpacingProperty, GridItemsLayout.VerticalItemSpacingProperty))
{
UpdateItemSpacing();
Expand Down Expand Up @@ -408,6 +417,7 @@ protected virtual void UpdateEmptyView()

_emptyViewAdapter.EmptyView = emptyView;
_emptyViewAdapter.EmptyViewTemplate = emptyViewTemplate;

Watch(ItemsViewAdapter);
}
else
Expand Down Expand Up @@ -479,7 +489,7 @@ void ScrollToRequested(object sender, ScrollToRequestEventArgs args)
protected virtual void ScrollTo(ScrollToRequestEventArgs args)
{
var position = DeterminePosition(args);

if (args.IsAnimated)
{
ScrollHelper.AnimateScrollToPosition(position, args.ScrollToPosition);
Expand Down