Skip to content

Commit

Permalink
Fix Android CollectionVIew Header and Footer Template (#22889)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen authored Jun 7, 2024
1 parent a66d40c commit 818fb9d
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ protected override void BindTemplatedItemViewHolder(TemplatedItemViewHolder temp

void UpdateHasHeader()
{
ItemsSource.HasHeader = ItemsView.Header != null;
ItemsSource.HasHeader = (ItemsView.Header ?? ItemsView.HeaderTemplate) is not null;
}

void UpdateHasFooter()
{
ItemsSource.HasFooter = ItemsView.Footer != null;
ItemsSource.HasFooter = (ItemsView.Footer ?? ItemsView.FooterTemplate) is not null;
}

bool IsHeader(int position)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#if ANDROID
// https://github.com/dotnet/maui/issues/22892
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue8761 : _IssuesUITest
{
public override string Issue => "CollectionView Header Template and Footer Template don't work";

public Issue8761(TestDevice device)
: base(device)
{ }

[Test]
[Category(UITestCategories.CollectionView)]
public void CollectionViewHeaderTemplateAndFooterTemplateDontWork()
{
for( int i = 0; i < 4; i++)
{
App.WaitForElement("AddItem");
App.Tap("AddItem");
App.WaitForElement("FooterLabel");
}
}
}
#endif
37 changes: 37 additions & 0 deletions src/Controls/tests/TestCases/Issues/Issue8761.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiCollectionViewFooterBug"
x:Class="Maui.Controls.Sample.Issues.Issue8761">

<CollectionView ItemsSource="{Binding Items}">
<CollectionView.HeaderTemplate>
<DataTemplate>
<Button
HorizontalOptions="Center"
Margin="12,24"
Text="Add Item"
AutomationId="AddItem"
Clicked="AddItem" />
</DataTemplate>
</CollectionView.HeaderTemplate>

<CollectionView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding Identifier}" Margin="12,0" />
</DataTemplate>
</CollectionView.ItemTemplate>

<CollectionView.FooterTemplate>
<DataTemplate>
<Label
Text="Footer"
HorizontalOptions="Center"
AutomationId="FooterLabel"
Margin="12,24" />
</DataTemplate>
</CollectionView.FooterTemplate>
</CollectionView>

</ContentPage>
27 changes: 27 additions & 0 deletions src/Controls/tests/TestCases/Issues/Issue8761.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.ObjectModel;
using Microsoft.Maui.Controls;

namespace Maui.Controls.Sample.Issues;

public sealed class ListItem
{
public string Identifier { get; } = Guid.NewGuid().ToString("D");
}

[Issue(IssueTracker.Github, 8761, "CollectionView Header Template and Footer Template don't work", PlatformAffected.Android)]
public partial class Issue8761 : ContentPage
{
public ObservableCollection<ListItem> Items { get; } = new();

public Issue8761()
{
BindingContext = this;
InitializeComponent();
}

private void AddItem(object sender, EventArgs e)
{
Items.Add(new ListItem());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,17 @@ CommandResponse CloseApp(IDictionary<string, object> parameters)
#pragma warning restore CS0618 // Type or member is obsolete
}
else
_app.Driver.TerminateApp(_app.GetAppId());
{
try
{
_app.Driver.TerminateApp(_app.GetAppId());
}
catch (Exception)
{
// Occasionally the app seems like it's already closed before we get here
// and then this throws an exception
}
}

return CommandResponse.SuccessEmptyResponse;
}
Expand Down

0 comments on commit 818fb9d

Please sign in to comment.