Skip to content

Commit

Permalink
Include headers/footers in EmptySource count so they show up when Ite…
Browse files Browse the repository at this point in the history
…msSource is null

Fixes #8934
  • Loading branch information
hartez committed Jul 3, 2023
1 parent 752c724 commit 6022c81
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace Microsoft.Maui.Controls.Handlers.Items
{
public class EmptyViewAdapter : RecyclerView.Adapter
{
int _headerHeight;
double _headerHeight;
int _headerViewType;
object _headerView;
DataTemplate _headerViewTemplate;

int _footerHeight;
double _footerHeight;
int _footerViewType;
object _footerView;
DataTemplate _footerViewTemplate;
Expand Down Expand Up @@ -301,18 +301,25 @@ void UpdateHeaderFooterHeight(object item, bool isHeader)
if (item == null)
return;

var sizeRequest = new SizeRequest(new Size(0, 0));
var size = Size.Zero;

if (item is View view)
sizeRequest = view.Measure(double.PositiveInfinity, double.PositiveInfinity, MeasureFlags.IncludeMargins);
if (item is IView view)
{
if (view.Handler == null)
{
TemplateHelpers.GetHandler(view as View, ItemsView.FindMauiContext());
}

size = view.Measure(double.PositiveInfinity, double.PositiveInfinity);
}

if (item is DataTemplate dataTemplate)
{
var content = dataTemplate.CreateContent() as View;
sizeRequest = content.Measure(double.PositiveInfinity, double.PositiveInfinity, MeasureFlags.IncludeMargins);
var content = dataTemplate.CreateContent() as IView;
size = content.Measure(double.PositiveInfinity, double.PositiveInfinity);
}

var itemHeight = (int)sizeRequest.Request.Height;
var itemHeight = size.Height;

if (isHeader)
_headerHeight = itemHeight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Microsoft.Maui.Controls.Handlers.Items
{
sealed internal class EmptySource : IItemsViewSource
{
public int Count => 0;
public int Count => (HasHeader? 1 : 0) + (HasFooter? 1 : 0);

public bool HasHeader { get; set; }
public bool HasFooter { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,37 @@ await CreateHandlerAndAddToWindow<IWindowHandler>(rootPage,
// Without Exceptions here, the test has passed.
Assert.Equal(0, (rootPage as IPageContainer<Page>).CurrentPage.Navigation.ModalStack.Count);
}

[Fact]
public async Task NullItemsSourceDisplaysHeaderFooterAndEmptyView()
{
SetupBuilder();

var emptyView = new Label { Text = "Empty" };
var header = new Label { Text = "Header" };
var footer = new Label { Text = "Footer" };

var collectionView = new CollectionView
{
ItemsSource = null,
EmptyView = emptyView,
Header = header,
Footer = footer
};

ContentPage contentPage = new ContentPage() { Content = collectionView };

var frame = collectionView.Frame;

await CreateHandlerAndAddToWindow<IWindowHandler>(contentPage,
async (_) =>
{
await WaitForUIUpdate(frame, collectionView);

Assert.True(emptyView.Height > 0, "EmptyView is not displayed");
Assert.True(header.Height > 0, "Header is not displayed");
Assert.True(footer.Height > 0, "Footer is not displayed");
});
}
}
}

0 comments on commit 6022c81

Please sign in to comment.