Fix horizontal layout issues introduced in 7456; fixes #7519 fixes #7524 (#7522)
authorE.Z. Hart <hartez@users.noreply.github.com>
Mon, 16 Sep 2019 14:23:55 +0000 (08:23 -0600)
committerRui Marinho <me@ruimarinho.net>
Mon, 16 Sep 2019 14:23:55 +0000 (15:23 +0100)
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7519.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7519Xaml.xaml [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7519Xaml.xaml.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Core/Items/CarouselView.cs
Xamarin.Forms.Core/Items/ListItemsLayout.cs
Xamarin.Forms.Core/Items/StructuredItemsView.cs
Xamarin.Forms.Platform.Android/CollectionView/CarouselViewRenderer.cs
Xamarin.Forms.Platform.Android/CollectionView/ItemsViewRenderer.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7519.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7519.cs
new file mode 100644 (file)
index 0000000..a8bc0eb
--- /dev/null
@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+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, 7519, "ItemSpacing not working on LinearLayout",
+               PlatformAffected.All)]
+       public class Issue7519 : TestNavigationPage
+       {
+               protected override void Init()
+               {
+#if APP
+                       FlagTestHelpers.SetCollectionViewTestFlag();
+                       PushAsync(new Issue7519Xaml());
+#endif
+               }
+       }
+}
diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7519Xaml.xaml b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7519Xaml.xaml
new file mode 100644 (file)
index 0000000..45aa3ac
--- /dev/null
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
+             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
+             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+             mc:Ignorable="d"
+             x:Class="Xamarin.Forms.Controls.Issues.Issue7519Xaml">
+    <ContentPage.Resources>
+        <ResourceDictionary>
+            <DataTemplate x:Key="_7519Template">
+                <Frame HeightRequest="200" WidthRequest="200" BackgroundColor="ForestGreen">
+                    <Label Text="{Binding Name}" VerticalOptions="Center" HorizontalOptions="Center" TextColor="White" />
+                </Frame>
+            </DataTemplate>
+        </ResourceDictionary>
+    </ContentPage.Resources>
+
+        <ContentPage.Content>
+        <StackLayout>
+
+            <Label Text="The CollectionView and CarouselView below should scroll horizontally and have 20 points of space between their items. If not, this test has failed."/>
+
+            <CollectionView ItemsSource="{Binding Items}" ItemTemplate="{StaticResource _7519Template}" Margin="20">
+                <CollectionView.ItemsLayout>
+                    <LinearItemsLayout ItemSpacing="20" Orientation="Horizontal"/>
+                </CollectionView.ItemsLayout>
+            </CollectionView>
+
+            <CarouselView ItemsSource="{Binding Items}" ItemTemplate="{StaticResource _7519Template}" Margin="20">
+                <CarouselView.ItemsLayout>
+                    <LinearItemsLayout ItemSpacing="20" Orientation="Horizontal" />
+                </CarouselView.ItemsLayout>
+            </CarouselView>
+            
+        </StackLayout>
+    </ContentPage.Content>
+</ContentPage>
\ No newline at end of file
diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7519Xaml.xaml.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7519Xaml.xaml.cs
new file mode 100644 (file)
index 0000000..e091c51
--- /dev/null
@@ -0,0 +1,43 @@
+using System.Collections.Generic;
+using Xamarin.Forms.Internals;
+using Xamarin.Forms.Xaml;
+
+namespace Xamarin.Forms.Controls.Issues
+{
+#if APP
+       [XamlCompilation(XamlCompilationOptions.Compile)]
+       public partial class Issue7519Xaml : ContentPage
+       {
+               public Issue7519Xaml()
+               {
+                       InitializeComponent();
+
+                       var items = new List<_7519ItemModel>()
+                       {
+                               new _7519ItemModel {Name = "Item 1"},
+                               new _7519ItemModel {Name = "Item 2"},
+                               new _7519ItemModel {Name = "Item 3"},
+                               new _7519ItemModel {Name = "Item 4"},
+                               new _7519ItemModel {Name = "Item 5"},
+                               new _7519ItemModel {Name = "Item 6"},
+                               new _7519ItemModel {Name = "Item 7"},
+                       };
+
+                       BindingContext = new _7519Model { Items = items };
+               }
+       }
+
+       [Preserve(AllMembers = true)]
+       public class _7519Model
+       {
+               public List<_7519ItemModel> Items { get; set; }
+       }
+
+       [Preserve(AllMembers = true)]
+       public class _7519ItemModel
+       {
+               public string Name { get; set; }
+       }
+
+#endif
+}
\ No newline at end of file
index 3870cba..652aab7 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Issue7049.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue7061.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue7111.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Issue7519.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Issue7519Xaml.xaml.cs">
+      <SubType>Code</SubType>
+    </Compile>
     <Compile Include="$(MSBuildThisFileDirectory)RefreshViewTests.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue7338.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)ScrollToGroup.cs" />
     <Compile Update="$(MSBuildThisFileDirectory)Issue6254.xaml.cs">
       <DependentUpon>Issue6254.xaml</DependentUpon>
     </Compile>
+    <Compile Update="C:\Users\hartez\Documents\Xamarin\Xamarin.Forms\Xamarin.Forms.Controls.Issues\Xamarin.Forms.Controls.Issues.Shared\Issue7519Xaml.xaml.cs">
+      <DependentUpon>Issue7519Xaml.xaml</DependentUpon>
+    </Compile>
   </ItemGroup>
   <ItemGroup>
     <EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue1455.xaml">
       <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
     </EmbeddedResource>
   </ItemGroup>
+  <ItemGroup>
+    <EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue7519Xaml.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </EmbeddedResource>
+  </ItemGroup>
 </Project>
\ No newline at end of file
index 0c9e9c7..a0266a2 100644 (file)
@@ -156,12 +156,12 @@ namespace Xamarin.Forms
 
                public static readonly BindableProperty ItemsLayoutProperty =
                        BindableProperty.Create(nameof(ItemsLayout), typeof(LinearItemsLayout), typeof(ItemsView),
-                               LinearItemsLayout.Horizontal);
+                               LinearItemsLayout.CarouselDefault);
 
                public LinearItemsLayout ItemsLayout
                {
-                       get => (LinearItemsLayout)InternalItemsLayout;
-                       set => InternalItemsLayout = value;
+                       get => (LinearItemsLayout)GetValue(ItemsLayoutProperty);
+                       set => SetValue(ItemsLayoutProperty, value);
                }
 
                public event EventHandler<CurrentItemChangedEventArgs> CurrentItemChanged;
index 5bcfa39..47ebdf4 100644 (file)
@@ -11,7 +11,10 @@ namespace Xamarin.Forms
                public static readonly IItemsLayout Vertical = new LinearItemsLayout(ItemsLayoutOrientation.Vertical); 
                public static readonly IItemsLayout Horizontal = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal);
 
-               // TODO hartez 2018/08/29 20:31:54 Need something like these previous two, but as a carousel default    
+               internal static readonly LinearItemsLayout CarouselDefault = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal)
+               {
+                       SnapPointsAlignment = SnapPointsAlignment.Center, SnapPointsType = SnapPointsType.Mandatory
+               };
 
                public static readonly BindableProperty ItemSpacingProperty =
                        BindableProperty.Create(nameof(ItemSpacing), typeof(double), typeof(LinearItemsLayout), default(double),
index 86e57a5..4b09ff0 100644 (file)
@@ -38,9 +38,7 @@
                        set => SetValue(FooterTemplateProperty, value);
                }
 
-               public static readonly BindableProperty ItemsLayoutProperty =
-                       BindableProperty.Create(nameof(ItemsLayout), typeof(IItemsLayout), typeof(ItemsView),
-                               LinearItemsLayout.Vertical);
+               public static readonly BindableProperty ItemsLayoutProperty = InternalItemsLayoutProperty;
 
                public IItemsLayout ItemsLayout
                {
index 8e1c46a..3c840cc 100644 (file)
@@ -9,7 +9,6 @@ namespace Xamarin.Forms.Platform.Android
        public class CarouselViewRenderer : ItemsViewRenderer<ItemsView, ItemsViewAdapter<ItemsView, IItemsViewSource>, IItemsViewSource>
        {
                protected CarouselView Carousel;
-               IItemsLayout _layout;
                ItemDecoration _itemDecoration;
                bool _isSwipeEnabled;
                bool _isUpdatingPositionFromForms;
@@ -30,8 +29,6 @@ namespace Xamarin.Forms.Platform.Android
                                        _itemDecoration.Dispose();
                                        _itemDecoration = null;
                                }
-
-                               _layout = null;
                        }
 
                        base.Dispose(disposing);
@@ -48,8 +45,6 @@ namespace Xamarin.Forms.Platform.Android
                                return;
                        }
 
-                       _layout = Carousel.ItemsLayout;
-
                        UpdateIsSwipeEnabled();
                        UpdateInitialPosition();
                        UpdateItemSpacing();
@@ -107,7 +102,7 @@ namespace Xamarin.Forms.Platform.Android
 
                protected override void UpdateItemSpacing()
                {
-                       if (_layout == null)
+                       if (ItemsLayout == null)
                        {
                                return;
                        }
@@ -117,7 +112,7 @@ namespace Xamarin.Forms.Platform.Android
                                RemoveItemDecoration(_itemDecoration);
                        }
 
-                       _itemDecoration = CreateSpacingDecoration(_layout);
+                       _itemDecoration = CreateSpacingDecoration(ItemsLayout);
                        AddItemDecoration(_itemDecoration);
 
                        var adapter = GetAdapter();
@@ -131,7 +126,6 @@ namespace Xamarin.Forms.Platform.Android
                        base.UpdateItemSpacing();
                }
 
-
                protected override IItemsLayout GetItemsLayout()
                {
                        return Carousel.ItemsLayout;
@@ -141,7 +135,7 @@ namespace Xamarin.Forms.Platform.Android
                {
                        var itemWidth = Width;
 
-                       if (_layout is LinearItemsLayout listItemsLayout && listItemsLayout.Orientation == ItemsLayoutOrientation.Horizontal)
+                       if (ItemsLayout is LinearItemsLayout listItemsLayout && listItemsLayout.Orientation == ItemsLayoutOrientation.Horizontal)
                        {
                                var numberOfVisibleItems = Carousel.NumberOfSideItems * 2 + 1;
                                itemWidth = (int)(Width - Carousel.PeekAreaInsets.Left - Carousel.PeekAreaInsets.Right - Context?.ToPixels(listItemsLayout.ItemSpacing)) / numberOfVisibleItems;
@@ -154,7 +148,7 @@ namespace Xamarin.Forms.Platform.Android
                {
                        var itemHeight = Height;
 
-                       if (_layout is LinearItemsLayout listItemsLayout && listItemsLayout.Orientation == ItemsLayoutOrientation.Vertical)
+                       if (ItemsLayout is LinearItemsLayout listItemsLayout && listItemsLayout.Orientation == ItemsLayoutOrientation.Vertical)
                        {
                                var numberOfVisibleItems = Carousel.NumberOfSideItems * 2 + 1;
                                itemHeight = (int)(Height - Carousel.PeekAreaInsets.Top - Carousel.PeekAreaInsets.Bottom - Context?.ToPixels(listItemsLayout.ItemSpacing)) / numberOfVisibleItems;
index d4fd3d7..34e35e8 100644 (file)
@@ -25,8 +25,8 @@ namespace Xamarin.Forms.Platform.Android
                bool _disposed;
 
                protected TItemsView ItemsView;
+               protected IItemsLayout ItemsLayout { get; private set; }
 
-               IItemsLayout _layout;
                SnapManager _snapManager;
                ScrollHelper _scrollHelper;
                RecyclerViewScrollListener<TItemsView, TItemsViewSource> _recyclerViewScrollListener;
@@ -303,8 +303,8 @@ namespace Xamarin.Forms.Platform.Android
 
                        UpdateItemsSource();
 
-                       _layout = GetItemsLayout();
-                       SetLayoutManager(SelectLayoutManager(_layout));
+                       ItemsLayout = GetItemsLayout();
+                       SetLayoutManager(SelectLayoutManager(ItemsLayout));
 
                        UpdateSnapBehavior();
                        UpdateBackgroundColor();
@@ -315,9 +315,9 @@ namespace Xamarin.Forms.Platform.Android
                        UpdateVerticalScrollBarVisibility();
 
                        // Keep track of the ItemsLayout's property changes
-                       if (_layout != null)
+                       if (ItemsLayout != null)
                        {
-                               _layout.PropertyChanged += LayoutPropertyChanged;
+                               ItemsLayout.PropertyChanged += LayoutPropertyChanged;
                        }
 
                        // Listen for ScrollTo requests
@@ -364,9 +364,9 @@ namespace Xamarin.Forms.Platform.Android
                        }
 
                        // Stop listening for layout property changes
-                       if (_layout != null)
+                       if (ItemsLayout != null)
                        {
-                               _layout.PropertyChanged -= LayoutPropertyChanged;
+                               ItemsLayout.PropertyChanged -= LayoutPropertyChanged;
                        }
 
                        // Stop listening for property changes
@@ -413,10 +413,10 @@ namespace Xamarin.Forms.Platform.Android
                        {
                                if (GetLayoutManager() is GridLayoutManager gridLayoutManager)
                                {
-                                       gridLayoutManager.SpanCount = ((GridItemsLayout)_layout).Span;
+                                       gridLayoutManager.SpanCount = ((GridItemsLayout)ItemsLayout).Span;
                                }
                        }
-                       else if (propertyChanged.IsOneOf(ItemsLayout.SnapPointsTypeProperty, ItemsLayout.SnapPointsAlignmentProperty))
+                       else if (propertyChanged.IsOneOf(Xamarin.Forms.ItemsLayout.SnapPointsTypeProperty, Xamarin.Forms.ItemsLayout.SnapPointsAlignmentProperty))
                        {
                                UpdateSnapBehavior();
                        }
@@ -438,7 +438,7 @@ namespace Xamarin.Forms.Platform.Android
                {
                        if (_snapManager == null)
                        {
-                               _snapManager = new SnapManager(_layout, this);
+                               _snapManager = new SnapManager(ItemsLayout, this);
                        }
                        return _snapManager;
                }
@@ -555,7 +555,7 @@ namespace Xamarin.Forms.Platform.Android
 
                protected virtual void UpdateItemSpacing()
                {
-                       if (_layout == null)
+                       if (ItemsLayout == null)
                        {
                                return;
                        }
@@ -565,7 +565,7 @@ namespace Xamarin.Forms.Platform.Android
                                RemoveItemDecoration(_itemDecoration);
                        }
 
-                       _itemDecoration = CreateSpacingDecoration(_layout);
+                       _itemDecoration = CreateSpacingDecoration(ItemsLayout);
                        AddItemDecoration(_itemDecoration);
                }
 
@@ -613,7 +613,7 @@ namespace Xamarin.Forms.Platform.Android
                        else if (!showEmptyView && currAdapter != ItemsViewAdapter)
                        {
                                SwapAdapter(ItemsViewAdapter, true);
-                               SetLayoutManager(SelectLayoutManager(_layout));
+                               SetLayoutManager(SelectLayoutManager(ItemsLayout));
                        }
                }