diff --git a/src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs b/src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs index 9239d5302eab..ab380324a700 100644 --- a/src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs +++ b/src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs @@ -571,46 +571,42 @@ void ClearLayoutListener() protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) { - if (Carousel.Loop) + // If the height or width are unbounded and the user is set to + // Loop then we can't just do an infinite measure. + // Looping works by setting item count to 16384 so if the + // CarV has infinite room it'll generate all 16384 items. + // This code forces the adapter to just measure the first item + // And then that measure is used for the WxH of the CarouselView + + // I found that "AtMost" also causes this behavior so + // that's why I'm turning "AtMost" into "Exactly" + if (MeasureSpec.GetMode(widthMeasureSpec) == MeasureSpecMode.AtMost) { - // If the height or width are unbounded and the user is set to - // Loop then we can't just do an infinite measure. - // Looping works by setting item count to 16384 so if the - // CarV has infinite room it'll generate all 16384 items. - // This code forces the adapter to just measure the first item - // And then that measure is used for the WxH of the CarouselView - - // I found that "AtMost" also causes this behavior so - // that's why I'm turning "AtMost" into "Exactly" - if (MeasureSpec.GetMode(widthMeasureSpec) == MeasureSpecMode.AtMost) - { - widthMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(widthMeasureSpec.GetSize()); - } + widthMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(widthMeasureSpec.GetSize()); + } - if (MeasureSpec.GetMode(heightMeasureSpec) == MeasureSpecMode.AtMost) - { - heightMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(heightMeasureSpec.GetSize()); - } + if (MeasureSpec.GetMode(heightMeasureSpec) == MeasureSpecMode.AtMost) + { + heightMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(heightMeasureSpec.GetSize()); + } - if (MeasureSpec.GetMode(widthMeasureSpec) == MeasureSpecMode.Unspecified || - MeasureSpec.GetMode(heightMeasureSpec) == MeasureSpecMode.Unspecified) + if (MeasureSpec.GetMode(widthMeasureSpec) == MeasureSpecMode.Unspecified || + MeasureSpec.GetMode(heightMeasureSpec) == MeasureSpecMode.Unspecified) + { + if (ItemsViewAdapter.ItemCount > 0) { - if (ItemsViewAdapter.ItemCount > 0) - { - // Retrieve the first item of the CarouselView and measure it - // This is what we'll use for the CarV WxH if the requested measure - // is for an infinite amount of space - - var viewType = ItemsViewAdapter.GetItemViewType(0); - var viewHolder = (ViewHolder)ItemsViewAdapter.CreateViewHolder(this, viewType); - ItemsViewAdapter.BindViewHolder(viewHolder, 0); - viewHolder.ItemView.Measure(widthMeasureSpec, heightMeasureSpec); - widthMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(viewHolder.ItemView.MeasuredWidth); - heightMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(viewHolder.ItemView.MeasuredHeight); - } + // Retrieve the first item of the CarouselView and measure it + // This is what we'll use for the CarV WxH if the requested measure + // is for an infinite amount of space + + var viewType = ItemsViewAdapter.GetItemViewType(0); + var viewHolder = (ViewHolder)ItemsViewAdapter.CreateViewHolder(this, viewType); + ItemsViewAdapter.BindViewHolder(viewHolder, 0); + viewHolder.ItemView.Measure(widthMeasureSpec, heightMeasureSpec); + widthMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(viewHolder.ItemView.MeasuredWidth); + heightMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(viewHolder.ItemView.MeasuredHeight); } } - base.OnMeasure(widthMeasureSpec, heightMeasureSpec); } diff --git a/src/Controls/tests/TestCases.Android.Tests/snapshots/android/CarouselViewShouldRenderCorrectly.png b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/CarouselViewShouldRenderCorrectly.png new file mode 100644 index 000000000000..8475af3988ba Binary files /dev/null and b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/CarouselViewShouldRenderCorrectly.png differ diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue25192.xaml b/src/Controls/tests/TestCases.HostApp/Issues/Issue25192.xaml new file mode 100644 index 000000000000..b66157fc115c --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue25192.xaml @@ -0,0 +1,105 @@ + + + + + + + Item1 + Item2 + Item3 + + + + + + + + + + + + + + + + + + +