summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRui Marinho <me@ruimarinho.net>2016-07-13 08:59:49 (GMT)
committerRui Marinho <me@ruimarinho.net>2016-07-13 09:45:42 (GMT)
commitf568ae25578489a3ffdfe8deb9688def64be3a9c (patch)
tree893124b754b48093b267b04c69c71c5b6f1626f9
parent7e65138fbacde0de1e5e8768a6c26783d6636f2c (diff)
downloadxamarin-forms-f568ae25578489a3ffdfe8deb9688def64be3a9c.zip
xamarin-forms-f568ae25578489a3ffdfe8deb9688def64be3a9c.tar.gz
xamarin-forms-f568ae25578489a3ffdfe8deb9688def64be3a9c.tar.bz2
[Android] Handle creating a default GroupHeader if no GroupHeaderTemplate is provided (#248)release-2.3.1beta-2.3.1-pre3
-rw-r--r--Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue55555.cs88
-rw-r--r--Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems1
-rw-r--r--Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs39
3 files changed, 113 insertions, 15 deletions
diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue55555.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue55555.cs
new file mode 100644
index 0000000..0aefc55
--- /dev/null
+++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue55555.cs
@@ -0,0 +1,88 @@
+´╗┐using System;
+using System.Collections.ObjectModel;
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+
+#if UITEST
+using Xamarin.UITest;
+using NUnit.Framework;
+#endif
+
+namespace Xamarin.Forms.Controls
+{
+ [Preserve(AllMembers = true)]
+ [Issue(IssueTracker.None, 55555, "Header problem")]
+ public class Issue55555 : TestContentPage // or TestMasterDetailPage, etc ...
+ {
+ protected override void Init()
+ {
+ var lstView = new ListView(ListViewCachingStrategy.RecycleElement);
+ ObservableCollection<GroupedVeggieModel> grouped = CreateData();
+
+ lstView.ItemsSource = grouped;
+ lstView.HasUnevenRows = true;
+ lstView.IsGroupingEnabled = true;
+ lstView.GroupDisplayBinding = new Binding("LongName");
+ lstView.GroupShortNameBinding = new Binding("ShortName");
+
+ lstView.ItemTemplate = new DataTemplate(typeof(DemoTextCell));
+ lstView.ItemTemplate.SetBinding(DemoTextCell.TextProperty, "Name");
+
+ Content = lstView;
+ }
+
+ static ObservableCollection<GroupedVeggieModel> CreateData()
+ {
+ var grouped = new ObservableCollection<GroupedVeggieModel>();
+
+ var veggieGroup = new GroupedVeggieModel() { LongName = "vegetables", ShortName = "v" };
+ veggieGroup.Add(new VeggieModel() { Name = "celery", IsReallyAVeggie = true, Comment = "try ants on a log" });
+ veggieGroup.Add(new VeggieModel() { Name = "tomato", IsReallyAVeggie = false, Comment = "pairs well with basil" });
+ veggieGroup.Add(new VeggieModel() { Name = "zucchini", IsReallyAVeggie = true, Comment = "zucchini bread > bannana bread" });
+ veggieGroup.Add(new VeggieModel() { Name = "peas", IsReallyAVeggie = true, Comment = "like peas in a pod" });
+
+ var fruitGroup = new GroupedVeggieModel() { LongName = "fruit", ShortName = "f" };
+ fruitGroup.Add(new VeggieModel() { Name = "banana", IsReallyAVeggie = false, Comment = "available in chip form factor" });
+ fruitGroup.Add(new VeggieModel() { Name = "strawberry", IsReallyAVeggie = false, Comment = "spring plant" });
+ fruitGroup.Add(new VeggieModel() { Name = "cherry", IsReallyAVeggie = false, Comment = "topper for icecream" });
+
+ grouped.Add(veggieGroup);
+ grouped.Add(fruitGroup);
+
+ return grouped;
+ }
+
+ [Preserve(AllMembers = true)]
+ public class VeggieModel
+ {
+ public string Name { get; set; }
+ public string Comment { get; set; }
+ public bool IsReallyAVeggie { get; set; }
+ public string Image { get; set; }
+ }
+
+ [Preserve(AllMembers = true)]
+ public class GroupedVeggieModel : ObservableCollection<VeggieModel>
+ {
+ public string LongName { get; set; }
+ public string ShortName { get; set; }
+ }
+
+ [Preserve(AllMembers = true)]
+ public class DemoTextCell : TextCell
+ {
+ public DemoTextCell()
+ {
+ Height = 150;
+ }
+ }
+
+#if UITEST
+ [Test]
+ public void TGroupDisplayBindingPresentRecycleElementTest()
+ {
+ RunningApp.WaitForElement(q => q.Marked("vegetables"));
+ }
+#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 7a7db40..38bf833 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
@@ -410,6 +410,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla41038.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla38284.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla39486.cs" />
+ <Compile Include="$(MSBuildThisFileDirectory)Issue55555.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">
diff --git a/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs b/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs
index 2230d2b..5dedef6 100644
--- a/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs
+++ b/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs
@@ -199,7 +199,7 @@ namespace Xamarin.Forms.Platform.Android
if (cachingStrategy == ListViewCachingStrategy.RecycleElement && convertView != null)
{
- var boxedCell = (INativeElementView)convertView;
+ var boxedCell = convertView as INativeElementView;
if (boxedCell == null)
{
throw new InvalidOperationException($"View for cell must implement {nameof(INativeElementView)} to enable recycling.");
@@ -403,20 +403,9 @@ namespace Xamarin.Forms.Platform.Android
if (global == position || cells.Count > 0)
{
- if (_listView.CachingStrategy == ListViewCachingStrategy.RecycleElement)
- {
- var groupContent = _listView.TemplatedItems.GroupHeaderTemplate?.CreateContent(group.ItemsSource, _listView) as Cell;
- if (groupContent != null)
- {
- groupContent.Parent = _listView;
- groupContent.BindingContext = group.ItemsSource;
- cells.Add(groupContent);
- }
- }
- else
- {
- cells.Add(group.HeaderContent);
- }
+ //Always create a new cell if we are using the RecycleElement strategy
+ var headerCell = _listView.CachingStrategy == ListViewCachingStrategy.RecycleElement ? GetNewGroupHeaderCell(group) : group.HeaderContent;
+ cells.Add(headerCell);
if (cells.Count == take)
return cells;
@@ -565,6 +554,26 @@ namespace Xamarin.Forms.Platform.Android
}
}
+ Cell GetNewGroupHeaderCell(ITemplatedItemsList<Cell> group)
+ {
+ var groupHeaderCell = _listView.TemplatedItems.GroupHeaderTemplate?.CreateContent(group.ItemsSource, _listView) as Cell;
+
+ if (groupHeaderCell != null)
+ {
+ groupHeaderCell.BindingContext = group.ItemsSource;
+ }
+ else
+ {
+ groupHeaderCell = new TextCell();
+ groupHeaderCell.SetBinding(TextCell.TextProperty, nameof(group.Name));
+ groupHeaderCell.BindingContext = group;
+ }
+
+ groupHeaderCell.Parent = _listView;
+ groupHeaderCell.SetIsGroupHeader<ItemsView<Cell>, Cell>(true);
+ return groupHeaderCell;
+ }
+
enum CellType
{
Row,