Skip to content

Conversation

@SyedAbdulAzeemSF4852
Copy link
Contributor

@SyedAbdulAzeemSF4852 SyedAbdulAzeemSF4852 commented Oct 22, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Issue Details

  • When a current item is set to a value that doesn't exist in the CarouselView's items source, the carousel incorrectly scrolls to the last item in a looped carousel.

Root Cause

CarouselViewHandler1 :

  • When CarouselView.CurrentItem is set to an item that is not present in the ItemsSource, ItemsSource.GetIndexForItem(invalidItem) returns -1, indicating that the item was not found. This -1 value is then passed through several methods: UpdateFromCurrentItem() calls ScrollToPosition(-1, currentPosition, animate), which triggers CarouselView.ScrollTo(-1, ...). In loop mode, this leads to CarouselViewHandler.ScrollToRequested being invoked with args.Index = -1. The handler then calls GetScrollToIndexPath(-1), which in turn invokes CarouselViewLoopManager.GetGoToIndex(collectionView, -1). Inside GetGoToIndex, arithmetic operations are performed on the invalid index, causing -1 to be treated as a valid position. As a result, the UICollectionView scrolls to this calculated physical position, which corresponds to the last logical item, producing unintended scroll behavior.

CarouselViewHandler2 :

  • When CurrentItem is not found in ItemsSource, GetIndexForItem returns -1; in loop mode, CarouselViewLoopManager.GetCorrectedIndexPathFromIndex(-1) adds 1, incorrectly converting it to 0, which results in an unintended scroll to the last duplicated item.

Description of Change

  • Added a check in ScrollToPosition methods in both CarouselViewController.cs and CarouselViewController2.cs to return early if goToPosition is less than zero, preventing unwanted scrolling when the target item is invalid.
  • NOTE : This PR resolves the issue of incorrect scrolling in loop mode when CurrentItem is not found in the ItemsSource, on Android.

Issues Fixed

Fixes #32139

Validated the behaviour in the following platforms

  • Windows
  • Android
  • iOS
  • Mac

Output

Before After
Before.mov
After.mov

@dotnet-policy-service dotnet-policy-service bot added community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration labels Oct 22, 2025
@sheiksyedm sheiksyedm added platform/ios area-controls-collectionview CollectionView, CarouselView, IndicatorView labels Oct 22, 2025
@SyedAbdulAzeemSF4852 SyedAbdulAzeemSF4852 marked this pull request as ready for review October 22, 2025 11:29
@Copilot Copilot AI review requested due to automatic review settings October 22, 2025 11:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug where CarouselView incorrectly scrolls to the last item in loop mode when CurrentItem is set to a value not present in ItemsSource. The fix adds early return checks when an invalid item index (-1) is detected, preventing the unwanted scroll behavior.

Key Changes:

  • Added validation to prevent scrolling when target position is invalid (< 0)
  • Implemented UI tests to verify the fix works correctly

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.

File Description
src/Controls/src/Core/Handlers/Items/iOS/CarouselViewController.cs Added guard clause to return early if goToPosition is negative
src/Controls/src/Core/Handlers/Items2/iOS/CarouselViewController2.cs Added guard clause to return early if goToPosition is negative
src/Controls/tests/TestCases.HostApp/Issues/Issue32139.cs Created UI test page demonstrating the issue with CarouselView loop mode
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue32139.cs Created NUnit test to validate no scroll occurs when selecting invalid item


void ScrollToPosition(int goToPosition, int carouselPosition, bool animate, bool forceScroll = false)
{
if (goToPosition < 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is < 0? "When CurrentItem is not found in ItemsSource, GetIndexForItem returns -1; in loop mode, CarouselViewLoopManager.GetCorrectedIndexPathFromIndex(-1) adds 1, incorrectly converting it to 0, which results in an unintended scroll to the last duplicated item." Could we apply fixes in the GetCorrectedIndexPathFromIndex directly?

Could we include logging? Silent failures are hard to debug.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsuarezruiz , I’ve updated the PR template for CV1.

We can handle this in GetCorrectedIndexPathFromIndex by creating an invalid NSIndexPath with the invalid index. However, I think it’s better for both CV1 and CV2 to return early from ScrollToPosition when goToPosition < 0, rather than allowing the invalid value to propagate through the scrolling call chain. This helps prevent unintended behavior earlier in the flow.

As for logging, I’ve added a debug log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-controls-collectionview CollectionView, CarouselView, IndicatorView community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/ios

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Android & iOS] Setting an invalid CurrentItem causes scroll to last item in looped CarouselView

3 participants