Skip to content

[Android] CarouselView: Fix scroll to last item after data reset in loop mode#31672

Open
SyedAbdulAzeemSF4852 wants to merge 4 commits intodotnet:mainfrom
SyedAbdulAzeemSF4852:fix-23023
Open

[Android] CarouselView: Fix scroll to last item after data reset in loop mode#31672
SyedAbdulAzeemSF4852 wants to merge 4 commits intodotnet:mainfrom
SyedAbdulAzeemSF4852:fix-23023

Conversation

@SyedAbdulAzeemSF4852
Copy link
Copy Markdown
Contributor

@SyedAbdulAzeemSF4852 SyedAbdulAzeemSF4852 commented Sep 18, 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 the item source is reloaded and items are added dynamically, updating the CurrentItem to one of the last items correctly changes the property, but the carousel fails to visually scroll to that position.

Root Cause:

  • When ScrollToPosition(carouselPosition) was called with a logical position like 0 (i.e., the CarouselView position), it scrolled to the raw adapter position 0. However, position 0 in the virtualized array represented the beginning of the full range, which was not the correct location for proper looping behavior. As a result, the center position was set to 0. Then, when attempting to scroll backward, the logic subtracted the target item index from this center position (0), leading to negative values.
  • This caused the scroll operation to fail, and no scrolling took place.

Description of Change

  • Updated GetGoToIndex in CarouselViewLoopManager.cs to correctly handle empty item sources and fix the calculation for determining the shortest scroll path in loop mode.
  • Added a new helper method UpdateLoopCentering to MauiCarouselRecyclerView.cs that recalculates and centers the carousel on the correct item when in loop mode, both after collection changes and during initial position updates. This ensures the view is properly centered even after resetting the data or changing the selected item.

Issues Fixed

Fixes #23023

Validated the behaviour in the following platforms

  • Windows - Test fails on Windows — tracked in #31670
  • 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 Sep 18, 2025
@jsuarezruiz jsuarezruiz added platform/android area-controls-collectionview CollectionView, CarouselView, IndicatorView labels Sep 18, 2025
@jsuarezruiz
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

@SyedAbdulAzeemSF4852 SyedAbdulAzeemSF4852 marked this pull request as ready for review October 17, 2025 04:53
Copilot AI review requested due to automatic review settings October 17, 2025 04:53
Copy link
Copy Markdown
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

Fixes incorrect centering/scrolling behavior of CarouselView (loop mode) on Android after the ItemsSource is reset and CurrentItem points to a tail item.
Key changes:

  • Adjusted loop index calculation in GetGoToIndex to use modular arithmetic and added bounds/empty checks.
  • Added UpdateLoopCentering helper and invoked it after collection changes and during initial position setup when looping.
  • Added a new UI test (Issue23023) and corresponding host page to reproduce and validate the scenario.

Reviewed Changes

Copilot reviewed 4 out of 7 changed files in this pull request and generated 3 comments.

File Description
src/Controls/src/Core/Handlers/Items/Android/CarouselViewLoopManager.cs Corrects loop navigation index calculations and adds input validation.
src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs Introduces UpdateLoopCentering to properly center after resets and refactors initial positioning logic.
src/Controls/tests/TestCases.HostApp/Issues/Issue23023.cs Adds a repro page with looping CarouselView and controls to reload items and scroll to last item.
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue23023.cs Adds UI test exercising reload + scroll-to-last-item scenario (currently wrapped in a conditional compilation symbol).

Comment thread src/Controls/tests/TestCases.HostApp/Issues/Issue23023.cs Outdated
Comment thread src/Controls/tests/TestCases.HostApp/Issues/Issue23023.cs
@jsuarezruiz
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Dec 9, 2025

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 31672

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 31672"

@rmarinho rmarinho added s/agent-review-incomplete AI agent could not complete all phases (blocker, timeout, error) s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix) s/agent-reviewed PR was reviewed by AI agent workflow (full 4-phase review) labels Feb 18, 2026
@SyedAbdulAzeemSF4852 SyedAbdulAzeemSF4852 changed the title [Android] Fix for CarouselView not scrolling to the last item after data reset despite CurrentItem being updated [Android] CarouselView: Fix scroll to last item after data reset in loop mode Feb 26, 2026
@SyedAbdulAzeemSF4852
Copy link
Copy Markdown
Contributor Author

Addressed concerns raised in the AI summary.

@dotnet dotnet deleted a comment from rmarinho Mar 15, 2026
@kubaflo kubaflo added s/agent-approved AI agent recommends approval - PR fix is correct and optimal s/agent-fix-pr-picked AI could not beat the PR fix - PR is the best among all candidates and removed s/agent-review-incomplete AI agent could not complete all phases (blocker, timeout, error) s/agent-approved AI agent recommends approval - PR fix is correct and optimal s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix) labels Mar 15, 2026
@MauiBot MauiBot added the s/agent-changes-requested AI agent recommends changes - found a better alternative or issues label Mar 29, 2026
Copy link
Copy Markdown
Contributor

@kubaflo kubaflo left a comment

Choose a reason for hiding this comment

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

The test failed

@SyedAbdulAzeemSF4852
Copy link
Copy Markdown
Contributor Author

The test failed

@kubaflo , I checked the test locally — it passes with the fix and fails without it. I’ve added the test output below for your reference.

With_Fix Without_Fix

@kubaflo
Copy link
Copy Markdown
Contributor

kubaflo commented Mar 31, 2026

/azp run maui-pr-uitests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@sheiksyedm
Copy link
Copy Markdown
Contributor

/azp run maui-pr-uitests , maui-pr-devicetests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

@MauiBot
Copy link
Copy Markdown
Collaborator

MauiBot commented Apr 22, 2026

🧪 UI Test Results

🧪 a9e40b6 · Fix formatting and remove hardcoded index value · 2026-04-22 18:41 UTC

Build #1391112 | 🔄 inProgress | / passed (0%)

🎯 Detected categories: CarouselView,CollectionView — ran 17 of 143 matrix cells (skipped 126)

@MauiBot MauiBot added s/agent-changes-requested AI agent recommends changes - found a better alternative or issues s/agent-fix-win AI found a better alternative fix than the PR and removed s/agent-review-incomplete AI agent could not complete all phases (blocker, timeout, error) s/agent-fix-pr-picked AI could not beat the PR fix - PR is the best among all candidates labels Apr 22, 2026
@dotnet dotnet deleted a comment from MauiBot Apr 22, 2026
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/android s/agent-changes-requested AI agent recommends changes - found a better alternative or issues s/agent-fix-win AI found a better alternative fix than the PR s/agent-reviewed PR was reviewed by AI agent workflow (full 4-phase review)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Android] CarouselView many issues with CurrentItem bindings

9 participants