diff --git a/Xamarin.Forms.Platform.Android/CollectionView/CarouselViewRenderer.cs b/Xamarin.Forms.Platform.Android/CollectionView/CarouselViewRenderer.cs index bef659c869c..86187fc9181 100644 --- a/Xamarin.Forms.Platform.Android/CollectionView/CarouselViewRenderer.cs +++ b/Xamarin.Forms.Platform.Android/CollectionView/CarouselViewRenderer.cs @@ -119,11 +119,8 @@ protected override void TearDownOldElement(ItemsView oldElement) protected override void UpdateAdapter() { - // By default the CollectionViewAdapter creates the items at whatever size the template calls for - // But for the Carousel, we want it to create the items to fit the width/height of the viewport - // So we give it an alternate delegate for creating the views - var oldItemViewAdapter = ItemsViewAdapter; + UnsubscribeCollectionItemsSourceChanged(oldItemViewAdapter); if (oldItemViewAdapter != null) { @@ -131,25 +128,28 @@ protected override void UpdateAdapter() Carousel.SetValueFromRenderer(FormsCarouselView.CurrentItemProperty, null); } - ItemsViewAdapter = new CarouselViewAdapter(Carousel, - (view, context) => new SizedItemContentView(Context, GetItemWidth, GetItemHeight)); - _gotoPosition = -1; - SwapAdapter(ItemsViewAdapter, false); + base.UpdateAdapter(); UpdateInitialPosition(); if (ItemsViewAdapter?.ItemsSource is ObservableItemsSource observableItemsSource) observableItemsSource.CollectionItemsSourceChanged += CollectionItemsSourceChanged; + } - oldItemViewAdapter?.Dispose(); + protected override ItemsViewAdapter CreateAdapter() + { + // By default the CollectionViewAdapter creates the items at whatever size the template calls for + // But for the Carousel, we want it to create the items to fit the width/height of the viewport + // So we give it an alternate delegate for creating the views + return new CarouselViewAdapter(Carousel, + (view, context) => new SizedItemContentView(Context, GetItemWidth, GetItemHeight)); } - protected override void UpdateItemsSource() + protected override void UpdateItemsSource() { - UpdateAdapter(); - UpdateEmptyView(); + base.UpdateItemsSource(); _carouselViewLoopManager.SetItemsSource(ItemsViewAdapter.ItemsSource); }