Skip to content
25 changes: 25 additions & 0 deletions src/Controls/src/Core/Handlers/Items/Android/ItemContentView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
handler.LayoutVirtualView(l, t, r, b);
}

// Ensure the ContentView's Frame is updated with the final layout bounds
// This is especially important during scrolling scenarios in CollectionView
if (View is ContentView contentView)
Copy link
Owner

Choose a reason for hiding this comment

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

This issue isn't specific to ContentView — it's caused by the hierarchical structure of the item templates. CollectionView does not measure all the nested elements within each item, which leads to incorrect sizing or layout behavior.

Copy link
Author

Choose a reason for hiding this comment

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

You're absolutely right. I've updated the fix to address the broader hierarchical structure issue rather than targeting just ContentView. The fix now ensures that any View in the item template hierarchy has its Frame properly updated before child measurements, addressing the root cause where CollectionView doesn't properly measure all nested elements. Commit 7a36e18 makes this change.

{
var currentFrame = contentView.Frame;
var newFrame = new Graphics.Rect(this.FromPixels(l), this.FromPixels(t),
this.FromPixels(r - l), this.FromPixels(b - t));
if (currentFrame != newFrame)
{
contentView.Frame = newFrame;
}
}
}

protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
Expand Down Expand Up @@ -204,6 +217,18 @@ _pixelSize is not null &&

ReportMeasure?.Invoke(new Size(pixelWidth, pixelHeight));

// Update the ContentView's Frame so that Width and Height properties are available
// to child elements during layout calculations in CollectionView scenarios
Copy link
Owner

Choose a reason for hiding this comment

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

Still we are not properly measuring the inner child

Copy link
Author

Choose a reason for hiding this comment

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

Fixed the child measurement timing issue by moving the ContentView Frame update to happen BEFORE the View.Measure() call instead of after. This ensures child elements have access to their parent's Width and Height properties during their own measure calculations. Commit ba5d0ac addresses this.

if (View is ContentView contentView)
{
var currentFrame = contentView.Frame;
var newFrame = new Graphics.Rect(currentFrame.X, currentFrame.Y, width, height);
if (currentFrame != newFrame)
{
contentView.Frame = newFrame;
}
}

SetMeasuredDimension(pixelWidth, pixelHeight);
}

Expand Down
25 changes: 0 additions & 25 deletions src/Core/src/Platform/Android/ContentViewGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,6 @@ protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
platformWidth = Math.Max(MinimumWidth, platformWidth);
platformHeight = Math.Max(MinimumHeight, platformHeight);

// Update the ContentView's Frame so that Width and Height properties are available
// to child elements during layout calculations, especially in CollectionView scenarios
if (CrossPlatformLayout is IView contentView)
{
var currentFrame = contentView.Frame;
var newFrame = new Graphics.Rect(currentFrame.X, currentFrame.Y, width, height);
if (currentFrame != newFrame)
{
contentView.Frame = newFrame;
}
}

SetMeasuredDimension((int)platformWidth, (int)platformHeight);
}

Expand All @@ -109,19 +97,6 @@ protected override void OnLayout(bool changed, int left, int top, int right, int
var destination = _context.ToCrossPlatformRectInReferenceFrame(left, top, right, bottom);

CrossPlatformArrange(destination);

// Ensure the ContentView's Frame is updated with the final layout bounds
// This is especially important during scrolling scenarios where the Frame
// might be reset or updated after the measure pass
if (CrossPlatformLayout is IView contentView)
{
var currentFrame = contentView.Frame;
var newFrame = destination;
if (currentFrame != newFrame)
{
contentView.Frame = newFrame;
}
}
}

internal IBorderStroke? Clip
Expand Down