[Android] Fixed issue changing the ItemTemplate on CarouselView (#7628) fixes #7395
authorJavier Suárez Ruiz <javiersuarezruiz@hotmail.com>
Fri, 27 Sep 2019 22:06:53 +0000 (00:06 +0200)
committerRui Marinho <me@ruimarinho.net>
Fri, 27 Sep 2019 22:06:53 +0000 (23:06 +0100)
* Fixed Issue 7395 - Changing ItemTemplate does not work as expected (Android)

* Added instructions to Issue7395

* Updated Core Gallery sample (instructions)

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7395.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.Android/CollectionView/ItemsViewRenderer.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7395.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7395.cs
new file mode 100644 (file)
index 0000000..010b9c4
--- /dev/null
@@ -0,0 +1,126 @@
+using System;
+using System.Collections.Generic;
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+
+#if UITEST
+using NUnit.Framework;
+using Xamarin.Forms.Core.UITests;
+using System.Linq;
+#endif
+
+namespace Xamarin.Forms.Controls.Issues
+{
+#if UITEST
+       [Category(UITestCategories.CollectionView)]
+#endif 
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Github, 7395, "Changing ItemTemplate does not work as expected", PlatformAffected.Android)]
+       public class Issue7395 : TestContentPage
+       {
+               CollectionView _collectionView;
+
+               public Issue7395()
+               {
+                       Title = "Issue 7395";
+               }
+
+               protected override void Init()
+               {
+                       var instructions = new Label
+                       {
+                               Text = "Click Two Columns. If all cells render correctly with 2 columns, then click Three Columns. If all cells render correctly with 3 columns, then the test passes."
+                       };
+
+                       var button1 = new Button
+                       {
+                               Text = "Two columns",
+                               AutomationId = "TwoCol"
+                       };
+
+                       button1.Clicked += OnButton1Clicked;
+
+                       var button2 = new Button
+                       {
+                               Text = "Three columns",
+                               AutomationId = "ThreeCol"
+                       };
+
+                       button2.Clicked += OnButton2Clicked;
+
+                       _collectionView = new CollectionView
+                       {
+                               BackgroundColor = Color.LightGreen,
+                               SelectionMode = SelectionMode.None,
+                               HeightRequest = 500
+                       };
+
+                       var lines = new List<Issue7395Model>();
+
+                       for (int i = 0; i < 30; i++)
+                       {
+                               lines.Add(new Issue7395Model() { Text = i.ToString() });
+                       }
+
+                       _collectionView.ItemsSource = lines;
+
+                       var stack = new StackLayout();
+
+                       stack.Children.Add(instructions);
+                       stack.Children.Add(button1);
+                       stack.Children.Add(button2);
+                       stack.Children.Add(_collectionView);
+
+                       Content = stack;
+               }
+
+               void OnButton1Clicked(object sender, EventArgs e)
+               {
+                       _collectionView.ItemTemplate = CreateDataGridTemplate(2);
+               }
+
+               void OnButton2Clicked(object sender, EventArgs e)
+               {
+                       _collectionView.ItemTemplate = CreateDataGridTemplate(3);
+               }
+
+               DataTemplate CreateDataGridTemplate(int columns)
+               {
+                       DataTemplate template = new DataTemplate(() =>
+                       {
+                               var grid = new Grid() { Padding = new Thickness(0), Margin = 0, RowSpacing = 0, ColumnSpacing = 0 };
+
+                               grid.RowDefinitions.Clear();
+                               grid.ColumnDefinitions.Clear();
+                               grid.Children.Clear();
+                               grid.RowDefinitions.Add(new RowDefinition() { Height = 40 });
+                               grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
+                               Label cell;
+                               cell = new Label() { };
+                               cell.SetBinding(Label.TextProperty, "Text");
+                               cell.FontSize = 20;
+                               cell.FontAttributes = FontAttributes.Bold;
+                               cell.BackgroundColor = Color.LightBlue;
+                               grid.Children.Add(cell, 0, 0);
+
+                               for (int i = 0; i < columns; i++)
+                               {
+                                       grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
+                                       cell = new Label() { };
+                                       cell.Text = "Col:" + i;
+                                       cell.FontAttributes = FontAttributes.Bold;
+                                       cell.BackgroundColor = Color.Beige;
+                                       grid.Children.Add(cell, i + 1, 0);
+                               }
+                               return grid;
+                       });
+                       return template;
+               }
+       }
+
+       [Preserve(AllMembers = true)]
+       public class Issue7395Model
+       {
+               public string Text { get; set; }
+       }
+}
index c4eb204..a444b3c 100644 (file)
       <SubType>Code</SubType>
     </Compile>
     <Compile Include="$(MSBuildThisFileDirectory)Issue7525.xaml.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Issue7395.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue7582.cs" />
   </ItemGroup>
   <ItemGroup>
index 3c9a6ba..35a91c9 100644 (file)
@@ -213,6 +213,10 @@ namespace Xamarin.Forms.Platform.Android
                        {
                                UpdateItemsSource();
                        }
+                       else if (changedProperty.Is(Xamarin.Forms.ItemsView.ItemTemplateProperty))
+                       {
+                               UpdateAdapter();
+                       }
                        else if (changedProperty.Is(VisualElement.BackgroundColorProperty))
                        {
                                UpdateBackgroundColor();
@@ -221,7 +225,7 @@ namespace Xamarin.Forms.Platform.Android
                        {
                                UpdateFlowDirection();
                        }
-                       else if (changedProperty.IsOneOf(Xamarin.Forms.ItemsView.EmptyViewProperty, 
+                       else if (changedProperty.IsOneOf(Xamarin.Forms.ItemsView.EmptyViewProperty,
                                Xamarin.Forms.ItemsView.EmptyViewTemplateProperty))
                        {
                                UpdateEmptyView();
@@ -276,7 +280,10 @@ namespace Xamarin.Forms.Platform.Android
                        ItemsViewAdapter = CreateAdapter();
 
                        if (GetAdapter() != _emptyViewAdapter)
+                       {
+                               SetAdapter(null);
                                SwapAdapter(ItemsViewAdapter, true);
+                       }
 
                        oldItemViewAdapter?.Dispose();
                }