-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix CarouselView PreviousPosition updating when inserting items #29617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
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. |
|
/azp run MAUI-UITests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
| // 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) |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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.
|
/azp run MAUI-UITests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
| var shouldUpdatePosition = currentItemPosition != carouselPosition && ItemsView.ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepItemsInView && InitialPositionSet && currentItemPosition > -1; | ||
| if (shouldUpdatePosition) | ||
| { | ||
| ItemsView.SetValueFromRenderer(CarouselView.PositionProperty, currentItemPosition); |
There was a problem hiding this comment.
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;
}There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
/azp run MAUI-UITests-public |
|
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; |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
|
/rebase |
39ec846 to
1db2db7
Compare
There was a problem hiding this 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) |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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?
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
Issues Fixed
Fixes #29524
Output ScreenShot
29524-BeforeFix.mov
29524-AfterFix.mov