Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure an exact measure for the nested horizontal scrollview #13554

Merged
merged 6 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Core/src/Platform/Android/MauiScrollView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Android.Animation;
using Android.Content;
using Android.Graphics;
using Android.Hardware.Lights;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this happens to me sometimes, is VS is adding this without us noticing ?

using Android.Runtime;
using Android.Util;
using Android.Views;
Expand Down Expand Up @@ -189,12 +190,20 @@ protected override void OnLayout(bool changed, int left, int top, int right, int

if (_hScrollView?.Parent == this && _content is not null)
{
double scrollViewContentHeight = _content.Height;
var scrollViewContentHeight = _content.Height;
var hScrollViewHeight = bottom - top;
var hScrollViewWidth = right - left;

//if we are scrolling both ways we need to lay out our MauiHorizontalScrollView with more than the available height
//so its parent the NestedScrollView can scroll vertically
var newBottom = _isBidirectional ? Math.Max(hScrollViewHeight, scrollViewContentHeight) : hScrollViewHeight;
_hScrollView.Layout(0, 0, right - left, (int)newBottom);
hScrollViewHeight = _isBidirectional ? Math.Max(hScrollViewHeight, scrollViewContentHeight) : hScrollViewHeight;

// Ensure that the horizontal scrollview has been measured at the actual target size
// so that it updates its internal bookkeeping to use the full size
_hScrollView.Measure(MeasureSpec.MakeMeasureSpec(right - left, MeasureSpecMode.Exactly),
MeasureSpec.MakeMeasureSpec(hScrollViewHeight, MeasureSpecMode.Exactly));

_hScrollView.Layout(0, 0, hScrollViewWidth, hScrollViewHeight);
}

if (CrossPlatformArrange == null)
Expand Down Expand Up @@ -301,6 +310,7 @@ protected MauiHorizontalScrollView(IntPtr javaReference, JniHandleOwnership tran
public MauiHorizontalScrollView(Context? context, MauiScrollView parentScrollView) : base(context)
{
_parentScrollView = parentScrollView;
Tag = "Microsoft.Maui.Android.HorizontalScrollView";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

public MauiHorizontalScrollView(Context? context, IAttributeSet? attrs) : base(context, attrs)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Android.Widget;
using AndroidX.AppCompat.Widget;
using AndroidX.Core.Widget;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Xunit;
using static Android.Views.View;

namespace Microsoft.Maui.DeviceTests
{
Expand Down Expand Up @@ -99,5 +101,33 @@ public async Task VerticalVisibilityInitializesCorrectly(ScrollBarVisibility vis

Assert.Equal(expected, result);
}

[Fact]
public async Task MauiScrollViewGetsFullHeightInHorizontalOrientation()
{
await InvokeOnMainThreadAsync(() =>
{
var sv = new MauiScrollView(MauiContext.Context);
sv.SetOrientation(ScrollOrientation.Horizontal);
var content = new Button(MauiContext.Context);
sv.SetContent(content);

var hsv = sv.FindViewWithTag("Microsoft.Maui.Android.HorizontalScrollView") as MauiHorizontalScrollView;

Assert.NotNull(hsv);

sv.Measure(
MeasureSpec.MakeMeasureSpec(1000, Android.Views.MeasureSpecMode.Exactly),
MeasureSpec.MakeMeasureSpec(1000, Android.Views.MeasureSpecMode.Exactly));

sv.Layout(0, 0, 1000, 1000);

var measuredWidth = hsv.MeasuredWidth;
var measuredHeight = hsv.MeasuredHeight;

Assert.Equal(1000, measuredWidth);
Assert.Equal(1000, measuredHeight);
});
}
}
}