Skip to content

Commit 173ca2b

Browse files
[Android] CarouselView: Remove rounding from SizedItemContentView that results in off-by-one pixel error (dotnet#25411)
* Remove rounding from SizedItemContentView that results in off-by-one pixel error * Don't override position for collection change notification if explicit scroll was performed. * [tests] Fix screenshots for CarouselView tests * Update test * Update screenshots * [test] Update uitest images * main should not use net10 --------- Co-authored-by: Rui Marinho <me@ruimarinho.net>
1 parent ad5f635 commit 173ca2b

File tree

7 files changed

+13
-5
lines changed

7 files changed

+13
-5
lines changed

src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class MauiCarouselRecyclerView : MauiRecyclerView<CarouselView, ItemsView
1313
CarouselViewLoopManager _carouselViewLoopManager;
1414
int _oldPosition;
1515
int _gotoPosition = -1;
16+
int _scrollToCounter = 0;
1617
bool _noNeedForScroll;
1718
bool _initialized;
1819
bool _isVisible;
@@ -183,6 +184,8 @@ public override void ScrollTo(ScrollToRequestEventArgs args)
183184
if (_carouselViewLoopManager == null)
184185
return;
185186

187+
_scrollToCounter++;
188+
186189
// Special case here
187190
// We could have a race condition where we are scrolling our collection to center the first item
188191
// And at the same time the user is requesting we go to a particular item
@@ -224,6 +227,7 @@ void CollectionItemsSourceChanged(object sender, System.Collections.Specialized.
224227
var carouselPosition = Carousel.Position;
225228
var currentItemPosition = observableItemsSource.GetPosition(Carousel.CurrentItem);
226229
var count = observableItemsSource.Count;
230+
var savedScrollToCounter = _scrollToCounter;
227231

228232
bool removingCurrentElement = currentItemPosition == -1;
229233
bool removingLastElement = e.OldStartingIndex == count;
@@ -273,9 +277,13 @@ void CollectionItemsSourceChanged(object sender, System.Collections.Specialized.
273277
GetDispatcher()
274278
.Dispatch(() =>
275279
{
276-
277-
SetCurrentItem(carouselPosition);
278-
UpdatePosition(carouselPosition);
280+
// If someone called explicit ScrollTo before the dispatched
281+
// callback was delivered then don't override it.
282+
if (_scrollToCounter == savedScrollToCounter)
283+
{
284+
SetCurrentItem(carouselPosition);
285+
UpdatePosition(carouselPosition);
286+
}
279287

280288
//If we are adding or removing the last item we need to update
281289
//the inset that we give to items so they are centered

src/Controls/src/Core/Handlers/Items/Android/SizedItemContentView.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
2929
double targetHeight = _height();
3030

3131
if (!double.IsInfinity(targetWidth))
32-
targetWidth = (int)Context.FromPixels(targetWidth);
32+
targetWidth = Context.FromPixels(targetWidth);
3333

3434
if (!double.IsInfinity(targetHeight))
35-
targetHeight = (int)Context.FromPixels(targetHeight);
35+
targetHeight = Context.FromPixels(targetHeight);
3636

3737
if (Content.VirtualView.Handler is IPlatformViewHandler pvh)
3838
{
-16 Bytes
Loading
-302 Bytes
Loading
-29.8 KB
Loading
-68 Bytes
Loading
0 Bytes
Loading

0 commit comments

Comments
 (0)