KEMBAR78
Fix CarouselView PreviousPosition updating when inserting items by Shalini-Ashokan · Pull Request #29617 · dotnet/maui · GitHub
Skip to content

Conversation

@Shalini-Ashokan
Copy link
Contributor

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

In the CarouselView control, when items are added at index 0, the PreviousPosition property in the PositionChangedEventArgs does not update correctly.

Root Cause

When items are inserted at index 0, explicitly setting the position to the current item position (currentItemPosition) prevents the actual position from changing. As a result, the PositionChanged event is not triggered, and PreviousPosition remains incorrect.

Description of Change

To correctly update the position and trigger the PositionChanged event, it update the PreviousPosition value correctly.

Validated the behavior in the following platforms

  • Android
  • Windows
  • iOS
  • Mac

Issues Fixed

Fixes #29524

Output ScreenShot

Before After
29524-BeforeFix.mov
29524-AfterFix.mov

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label May 22, 2025
@dotnet-policy-service
Copy link
Contributor

Hey there @@Shalini-Ashokan! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service dotnet-policy-service bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label May 22, 2025
@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jsuarezruiz jsuarezruiz added the area-controls-collectionview CollectionView, CarouselView, IndicatorView label May 22, 2025
// Updates position to match current item when inserting items, correctly tracking previous position values.
if (currentItemPosition != carouselPosition && ItemsView.ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepItemsInView)
{
if (InitialPositionSet && currentItemPosition > -1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can nest this two conditions in a single one, but would be better to create a variable to increase the readability:

 var shouldUpdatePosition = 
     currentItemPosition != carouselPosition &&
     ItemsView.ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepItemsInView &&
     InitialPositionSet && 
     currentItemPosition > -1;

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 have updated the code section based on your suggestions.

@Shalini-Ashokan Shalini-Ashokan marked this pull request as ready for review May 27, 2025 12:29
@Shalini-Ashokan Shalini-Ashokan requested a review from a team as a code owner May 27, 2025 12:29
@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@PureWeen PureWeen added this to the .NET 9 SR9 milestone Jun 9, 2025
@PureWeen PureWeen moved this from Todo to Ready To Review in MAUI SDK Ongoing Jun 9, 2025
var shouldUpdatePosition = currentItemPosition != carouselPosition && ItemsView.ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepItemsInView && InitialPositionSet && currentItemPosition > -1;
if (shouldUpdatePosition)
{
ItemsView.SetValueFromRenderer(CarouselView.PositionProperty, currentItemPosition);
Copy link
Member

Choose a reason for hiding this comment

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

Should this move into the GetPositionWhenAddingItems method for consistency?

We have setters in the GetPositionWhenResetItems method:

int GetPositionWhenResetItems()
{
	//If we are reseting the collection Position should go to 0
	ItemsView?.SetValueFromRenderer(CarouselView.CurrentItemProperty, null);
	return 0;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mattleibow, As per your suggestion, I have moved the logic into the GetPositionWhenAddingItems method for consistency.

if (e.Action == NotifyCollectionChangedAction.Add)
{
// Updates position to match current item when inserting items, correctly tracking previous position values.
var shouldUpdatePosition = currentItemPosition != carouselPosition && ItemsView.ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepItemsInView && InitialPositionSet && currentItemPosition > -1;
Copy link
Member

Choose a reason for hiding this comment

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

What about KeepLastItemInView or KeepScrollOffset? Those also feel like candidates.

I found some Xamarin.Forms docs, so maybe just add this to the enum for now:

KeepItemsInView keeps the first item in the list displayed when new items are added.
KeepScrollOffset ensures that the current scroll position is maintained when new items are added.
KeepLastItemInView adjusts the scroll offset to keep the last item in the list displayed when new items are added.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mattleibow, In CarouselView, the PreviousPosition is updated correctly for both KeepLastItemInView and KeepScrollOffset modes, so I added the condition only for the KeepItemsInView mode.

As per your suggestion, I have also added a summary for the ItemsUpdatingScrollMode enum.

@github-project-automation github-project-automation bot moved this from Ready To Review to Changes Requested in MAUI SDK Ongoing Jun 27, 2025
@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

int GetPositionWhenAddingItems(int carouselPosition, int currentItemPosition)
{
// Updates position to match current item when inserting items, correctly tracking previous position values.
var shouldUpdatePosition = currentItemPosition != carouselPosition && ItemsView.ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepItemsInView && InitialPositionSet && currentItemPosition > -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we split this big condition?

Apply an early exit strategy:

const int InvalidPosition = -1;

// Early return for invalid positions - maintain current carousel position
if (currentItemPosition == InvalidPosition)
    return carouselPosition;

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 have split the condition and updated the code to return the carouselPosition value earlier.


[Test]
[Category(UITestCategories.CarouselView)]
public void VerifyPreviousPositionUpdatesCorrectlyWhenInsertingItems()
Copy link
Contributor

Choose a reason for hiding this comment

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

This suggestion is optional. These tests are a really good start, but could we include a few more?

  • VerifyPositionUpdatesWhenRemovingCurrentItem
  • VerifyPositionAfterBulkInsert (Add a Button in the sample to add several items at once)
  • VerifyPositionAfterBulkRemove

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, In a scenario where ItemsUpdatingScrollMode is set to KeepItemsInView, inserting 5 items at the beginning of the list results in the previous position being 5 on Android and 1 on iOS.
Could you please confirm which behavior is correct?

Copy link
Contributor

Choose a reason for hiding this comment

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

Initial State:
Suppose we have items: [A, B, C, D, E]
Current position is at index 0 (item A)

Operation:
Insert 5 items at the beginning: [X1, X2, X3, X4, X5, A, B, C, D, E]
The current item (A) is now at index 5

Expected PositionChangedEventArgs:
PreviousPosition: 0 (original position of A)
CurrentPosition: 5 (new position of A)

Can you validate it?

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, Currently, when ItemsUpdatingScrollMode is set to KeepItemsInView and 5 items are added at the beginning [X1, X2, X3, X4, X5], the resulting collection becomes [X1, X2, X3, X4, X5, A, B, C, D, E].

PositionChangedEventArgs:
CurrentPosition: 0 (new position of X1)
PreviousPosition: 9 (original position of A)

This happens because the PositionChanged event is invoked for each item inserted.

Expected PositionChangedEventArgs:
CurrentPosition: 0 (new position of A)
PreviousPosition: 5 (original position of A)

What is the correct behavior in this scenario?

@PureWeen PureWeen modified the milestones: .NET 9 SR9, .NET 9 SR10 Jul 3, 2025
@PureWeen PureWeen modified the milestones: .NET 9 SR10, .NET 9 SR11 Aug 4, 2025
@jsuarezruiz
Copy link
Contributor

/rebase

@Copilot Copilot AI review requested due to automatic review settings September 8, 2025 07:14
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

int GetPositionWhenAddingItems(int carouselPosition, int currentItemPosition)
{
// Early return for invalid positions - maintain current carousel position
if (currentItemPosition < 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could also validate more edge cases? Like, position bigger than ItemSource.Count.


[Test]
[Category(UITestCategories.CarouselView)]
public void VerifyPreviousPositionUpdatesCorrectlyWhenInsertingItems()
Copy link
Contributor

Choose a reason for hiding this comment

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

Initial State:
Suppose we have items: [A, B, C, D, E]
Current position is at index 0 (item A)

Operation:
Insert 5 items at the beginning: [X1, X2, X3, X4, X5, A, B, C, D, E]
The current item (A) is now at index 5

Expected PositionChangedEventArgs:
PreviousPosition: 0 (original position of A)
CurrentPosition: 5 (new position of A)

Can you validate it?

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

Projects

Status: Changes Requested

Development

Successfully merging this pull request may close these issues.

[iOS] Previous Position in PositionChangedEventArgs Not Updating Correctly in CarouselView(CV2)

4 participants