diff --git a/src/Controls/src/Core/Handlers/Items/iOS/CarouselViewController.cs b/src/Controls/src/Core/Handlers/Items/iOS/CarouselViewController.cs index cd38138ab7e3..9163775f3359 100644 --- a/src/Controls/src/Core/Handlers/Items/iOS/CarouselViewController.cs +++ b/src/Controls/src/Core/Handlers/Items/iOS/CarouselViewController.cs @@ -302,7 +302,7 @@ void CarouselViewScrolled(object sender, ItemsViewScrolledEventArgs e) [UnconditionalSuppressMessage("Memory", "MEM0003", Justification = "Proven safe in test: MemoryTests.HandlerDoesNotLeak")] void CollectionViewUpdating(object sender, NotifyCollectionChangedEventArgs e) { - if (ItemsView is not CarouselView carousel) + if (ItemsSource.ItemCount == 0 || ItemsView is not CarouselView carousel) { return; } @@ -343,6 +343,18 @@ void CollectionViewUpdated(object sender, NotifyCollectionChangedEventArgs e) SetPosition(targetPosition); SetCurrentItem(targetPosition); + + if (e.Action == NotifyCollectionChangedAction.Remove) + { + //Since we can be removing the item that is already created next to the current item we need to update the visible cells + if (ItemsView.Loop) + { + CollectionView.ReloadItems(new NSIndexPath[] { GetScrollToIndexPath(targetPosition) }); + } + } + + ScrollToPosition(targetPosition,targetPosition,false,true); + } int GetPositionWhenAddingItems(int carouselPosition, int currentItemPosition) @@ -437,7 +449,7 @@ void ScrollToPosition(int goToPosition, int carouselPosition, bool animate, bool return; } - if (_gotoPosition == -1 && (goToPosition != carouselPosition || forceScroll)) + if (goToPosition != carouselPosition || forceScroll) { _gotoPosition = goToPosition; carousel.ScrollTo(goToPosition, position: Microsoft.Maui.Controls.ScrollToPosition.Center, animate: animate); @@ -729,7 +741,7 @@ public NSIndexPath GetGoToIndex(UICollectionView collectionView, int newPosition var incrementAbs = Math.Abs(increment); int goToPosition; - if (diffToStart < incrementAbs) + if (diffToStart <= incrementAbs) { goToPosition = centerIndexPath.Row - diffToStart; } @@ -830,7 +842,12 @@ void FinishCenterIfNeeded(UICollectionView collectionView, int shiftCells) int GetCorrectedIndex(int indexToCorrect) { var itemsCount = GetItemsSourceCount(); - if ((indexToCorrect < itemsCount && indexToCorrect >= 0) || itemsCount == 0) + if ( itemsCount == 0) + { + return 0; + } + + if (indexToCorrect < itemsCount && indexToCorrect >= 0) { return indexToCorrect; } diff --git a/src/Controls/tests/TestCases.HostApp/Issues/CarouselViewLoopNoFreeze.cs b/src/Controls/tests/TestCases.HostApp/Issues/CarouselViewLoopNoFreeze.cs index e135c6a92a6f..a4ddc7772e5e 100644 --- a/src/Controls/tests/TestCases.HostApp/Issues/CarouselViewLoopNoFreeze.cs +++ b/src/Controls/tests/TestCases.HostApp/Issues/CarouselViewLoopNoFreeze.cs @@ -13,7 +13,7 @@ public class CarouselViewLoopNoFreeze : ContentPage readonly string _btnRemoveAllAutomationId = "btnRemoveAll"; readonly ViewModelIssue12574 _viewModel; - readonly CarouselView2 _carouselView; + readonly CarouselView _carouselView; readonly Button _btn; readonly Button _btn2; @@ -33,7 +33,7 @@ public CarouselViewLoopNoFreeze() }; _btn2.SetBinding(Button.CommandProperty, "RemoveAllItemsCommand"); - _carouselView = new CarouselView2 + _carouselView = new CarouselView { AutomationId = _carouselAutomationId, Margin = new Thickness(30), @@ -178,4 +178,4 @@ protected void OnPropertyChanged(string propertyName) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } -} \ No newline at end of file +}