From 3b6462ba1f13bde7c0a631b310dbe014f57f4d66 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Fri, 31 May 2019 19:40:25 -0600 Subject: [PATCH] [Android] Pass correct measurement values to item content (#6285) * Test case to reproduce the issue * Set proper height/width values in measure for layouts; fixes #5765 * More descriptive test name * Avoid unnecessary FromPixel calls * Remove double include fixes #5765 fixes #5782 --- .../Issue5765.cs | 79 ++++++++++++++++++++++ .../Xamarin.Forms.Controls.Issues.Shared.projitems | 3 +- .../CollectionView/ItemContentView.cs | 15 ++-- 3 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5765.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5765.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5765.cs new file mode 100644 index 0000000..97effb5 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5765.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +#if UITEST +using Xamarin.Forms.Core.UITests; +using Xamarin.UITest; +using NUnit.Framework; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ +#if UITEST + [Category(UITestCategories.CollectionView)] +#endif + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 5765, "[Frame, CollectionView, Android]The Label.Text is invisible on Android if DataTemplate have frame as layout", + PlatformAffected.Android)] + class Issue5765 : TestNavigationPage + { + const string Target = "FirstLabel"; + + protected override void Init() + { + FlagTestHelpers.SetCollectionViewTestFlag(); + + PushAsync(CreateRoot()); + } + + Frame CreateFrame() + { + var frame = new Frame() { CornerRadius = 10, BackgroundColor = Color.SeaGreen }; + + var flexLayout = new FlexLayout() + { + Direction = FlexDirection.Row, + JustifyContent = FlexJustify.SpaceBetween, + AlignItems = FlexAlignItems.Stretch + }; + + var label1 = new Label { Text = "First Label", AutomationId = Target, HeightRequest = 100 }; + var label2 = new Label { Text = "Second Label" }; + + flexLayout.Children.Add(label1); + flexLayout.Children.Add(label2); + + frame.Content = flexLayout; + + return frame; + } + + Page CreateRoot() + { + var page = new ContentPage() { Title = "Issue5765" }; + + var cv = new CollectionView(); + + cv.ItemTemplate = new DataTemplate(() => { + return CreateFrame(); + }); + + cv.ItemsSource = new List { "one", "two", "three" }; + + page.Content = cv; + + return page; + } + +#if UITEST + [Test] + public void FlexLayoutsInFramesShouldSizeCorrectly() + { + // If the first label is visible at all, then this has succeeded + RunningApp.WaitForElement(Target); + } +#endif + } +} diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 6ad75ce..b8fd729 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -19,6 +19,7 @@ + Issue4992.xaml @@ -483,7 +484,7 @@ Code - Issue5949_2.xaml + Issue5949_2.xaml Code diff --git a/Xamarin.Forms.Platform.Android/CollectionView/ItemContentView.cs b/Xamarin.Forms.Platform.Android/CollectionView/ItemContentView.cs index ca2b541..ffea4f5 100644 --- a/Xamarin.Forms.Platform.Android/CollectionView/ItemContentView.cs +++ b/Xamarin.Forms.Platform.Android/CollectionView/ItemContentView.cs @@ -29,12 +29,12 @@ namespace Xamarin.Forms.Platform.Android { Content.Element.MeasureInvalidated -= ElementMeasureInvalidated; } - - if(Content?.View != null) + + if (Content?.View != null) { RemoveView(Content.View); } - + Content = null; } @@ -63,8 +63,13 @@ namespace Xamarin.Forms.Platform.Android int pixelWidth = MeasureSpec.GetSize(widthMeasureSpec); int pixelHeight = MeasureSpec.GetSize(heightMeasureSpec); - var width = Context.FromPixels(pixelWidth); - var height = Context.FromPixels(pixelHeight); + var width = MeasureSpec.GetMode(widthMeasureSpec) == MeasureSpecMode.Unspecified + ? double.PositiveInfinity + : Context.FromPixels(pixelWidth); + + var height = MeasureSpec.GetMode(heightMeasureSpec) == MeasureSpecMode.Unspecified + ? double.PositiveInfinity + : Context.FromPixels(pixelHeight); SizeRequest measure = Content.Element.Measure(width, height, MeasureFlags.IncludeMargins); -- 2.7.4