Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 16 additions & 47 deletions src/Controls/src/Core/Handlers/Items/Android/ItemContentView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System;
using Android.Content;
using Android.Views;
using AndroidX.CoordinatorLayout.Widget;
using AndroidX.Fragment.App;
using Microsoft.Maui.Graphics;
using AView = Android.Views.View;

Expand All @@ -23,26 +21,31 @@ public ItemContentView(Context context) : base(context)

internal void ClickOn() => CallOnClick();

AView PlatformView => Content?.ContainerView ?? Content?.PlatformView;

internal void RealizeContent(View view, ItemsView itemsView)
{
Content = CreateHandler(view, itemsView);
var platformView = Content.ContainerView ?? Content.PlatformView;
var platformView = PlatformView;

//make sure we don't belong to a previous Holder
platformView.RemoveFromParent();
AddView(platformView);

//TODO: RUI IS THIS THE BEST WAY TO CAST?
(View as VisualElement).MeasureInvalidated += ElementMeasureInvalidated;
if (View is VisualElement visualElement)
{
visualElement.MeasureInvalidated += ElementMeasureInvalidated;
}
}

internal void Recycle()
{
if (View != null)
if (View is VisualElement visualElement)
{
(View as VisualElement).MeasureInvalidated -= ElementMeasureInvalidated;
visualElement.MeasureInvalidated -= ElementMeasureInvalidated;
}

var platformView = Content?.ContainerView ?? Content?.PlatformView;
var platformView = PlatformView;

if (platformView != null)
{
Expand All @@ -66,18 +69,10 @@ protected override void OnLayout(bool changed, int l, int t, int r, int b)
return;
}

var size = this.FromPixels(r - l, b - t);

//TODO: RUI Is this the best way?
//View.Arrange(new Rectangle(Point.Zero, size));
//Arrange doesn't seem to work as expected

if (View?.Handler is not IPlatformViewHandler handler)
return;

handler.LayoutVirtualView(l, t, r, b);

UpdateContentLayout();
if (View?.Handler is IPlatformViewHandler handler)
{
handler.LayoutVirtualView(l, t, r, b);
}
}

protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
Expand Down Expand Up @@ -125,7 +120,7 @@ protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
SetMeasuredDimension(pixelWidth, pixelHeight);
}

void ElementMeasureInvalidated(object sender, System.EventArgs e)
void ElementMeasureInvalidated(object sender, EventArgs e)
{
if (this.IsAlive())
{
Expand All @@ -137,32 +132,6 @@ void ElementMeasureInvalidated(object sender, System.EventArgs e)
}
}

void UpdateContentLayout()
{
VisualElement mauiControlsView = View as VisualElement;
AView aview = Content.ToPlatform();

if (mauiControlsView == null || aview == null)
return;

var x = (int)this.ToPixels(mauiControlsView.X);
var y = (int)this.ToPixels(mauiControlsView.Y);
var width = Math.Max(0, (int)this.ToPixels(mauiControlsView.Width));
var height = Math.Max(0, (int)this.ToPixels(mauiControlsView.Height));

aview.Layout(x, y, width, height);

if ((aview is LayoutViewGroup || aview is ContentViewGroup || aview is CoordinatorLayout || aview is FragmentContainerView) && width == 0 && height == 0)
{
// Nothing to do here; just chill.
}
else
{
aview.Measure(MeasureSpecMode.Exactly.MakeMeasureSpec(width), MeasureSpecMode.Exactly.MakeMeasureSpec(height));
aview.Layout(x, y, x + width, y + height);
}
}

static IPlatformViewHandler CreateHandler(View view, ItemsView itemsView) =>
TemplateHelpers.GetHandler(view, itemsView.FindMauiContext());
}
Expand Down