Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
ScrollView re-layout when content layout changes; fixes #1760 and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hartez committed May 8, 2018
1 parent 8dde156 commit 9e47f08
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 1760, "Content set after an await is not visible", PlatformAffected.Android)]
public class Issue1760 : TestMasterDetailPage
{
const string Before = "Before";
const string After = "After";
const int Wait = 3;

protected override void Init()
{
Master = new _1760Master();
Detail = new _1760TestPage();
IsPresented = true;
}

[Preserve(AllMembers = true)]
public class _1760Master : ContentPage
{
public _1760Master()
{
var instructions = new Label { Text = $"Select one of the menu items. The detail page text should change to {Before}. After {Wait} seconds the text should change to {After}." };

var menuView = new ListView(ListViewCachingStrategy.RetainElement)
{
ItemsSource = new List<string> { "Test Page 1", "Test Page 2" }
};

menuView.ItemSelected += OnMenuClicked;

Content = new StackLayout{Children = { instructions, menuView }};
Title = "GH 1760 Test App";
}

void OnMenuClicked(object sender, SelectedItemChangedEventArgs e)
{
var mainPage = (MasterDetailPage)Parent;
mainPage.Detail = new _1760TestPage();
mainPage.IsPresented = false;
}
}

[Preserve(AllMembers = true)]
public class _1760TestPage : ContentPage
{
public async Task DisplayPage()
{
IsBusy = true;
HeaderPageContent = new Label {Text = Before, TextColor = Color.Black};

await Task.Delay(Wait * 1000);

HeaderPageContent = new Label { Text = After, TextColor = Color.Black};
IsBusy = false;
}

ContentView _headerPageContent;
public View HeaderPageContent
{
set => _headerPageContent.Content = value;
}

public _1760TestPage()
{
CreateHeaderPage();
DisplayPage();
}

void CreateHeaderPage()
{

_headerPageContent = new ContentView
{
Content = new Label { Text = "_1760 Test Page Content" },
BackgroundColor = Color.White,
Margin = 40
};

Title = "_1760 Test Page";

Content = new ScrollView
{
Content = _headerPageContent
};
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Timers;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 2595, "ScrollView.Content is not re-layouted on Android", PlatformAffected.Android)]
public class Issue2595 : TestMasterDetailPage
{
protected override void Init()
{
Master = new _2595Master();
Detail = new _2595ScrollPage();
IsPresented = true;
}

[Preserve(AllMembers = true)]
public class _2595Master : ContentPage
{
public _2595Master()
{
var instructions = new Label { Text = $"Select one of the menu items. The detail page text should "
+ $"display a label which disappears after 1 second and is"
+ $" replaced by an updating list of labels which grows vertically." };

var menuView = new ListView(ListViewCachingStrategy.RetainElement)
{
ItemsSource = new List<string> { "Test Page 1", "Test Page 2" }
};

menuView.ItemSelected += OnMenuClicked;

Content = new StackLayout{Children = { instructions, menuView }};
Title = "GH 2595 Test App";
}

void OnMenuClicked(object sender, SelectedItemChangedEventArgs e)
{
var mainPage = (MasterDetailPage)Parent;
mainPage.Detail = new _2595ScrollPage ();
mainPage.IsPresented = false;
}
}

[Preserve(AllMembers = true)]
public class _2595ScrollPage : ContentPage
{
readonly Timer _timer = new Timer(1000);
protected Label Label;

public _2595ScrollPage() {
Content = new ScrollView {

BackgroundColor = Color.Red,

Content = new StackLayout {
BackgroundColor = Color.BlueViolet,
Children = {
(Label = new Label {
Text = "this text should disappear after 1 sec",
BackgroundColor = Color.LightBlue,
HorizontalOptions = LayoutOptions.StartAndExpand,
})
}
}
};
}

protected StackLayout ScrollContent {
get => (Content as ScrollView).Content as StackLayout;
set => (Content as ScrollView).Content = value;
}

protected override void OnAppearing() {
base.OnAppearing();
_timer.Elapsed += (s, e) => Device.BeginInvokeOnMainThread(OnTimerElapsed);

_timer.Start();
}

void OnTimerElapsed() {
Label.Text = $"{ DateTime.Now.ToString() }: expecting {ScrollContent?.Children.Count} dates to show up.";
ScrollContent.Children.Add(new Label { Text = DateTime.Now.ToString() });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue1415.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2247.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GroupListViewHeaderIndexOutOfRange.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1760.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1975.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1601.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1717.cs" />
Expand Down Expand Up @@ -302,6 +303,8 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue1908.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1672.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2394.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1908.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2595.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2983.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2963.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2981.cs" />
Expand Down
17 changes: 17 additions & 0 deletions Xamarin.Forms.Platform.Android/Renderers/ScrollViewContainer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.ComponentModel;
using Android.Content;
using Android.Views;

Expand All @@ -23,6 +25,11 @@ public View ChildView

RemoveAllViews();

if (_childView is Layout layout1)
{
layout1.LayoutChanged -= OnLayoutChanged;
}

_childView = value;

if (_childView == null)
Expand All @@ -36,9 +43,19 @@ public View ChildView
renderer.View.RemoveFromParent();

AddView(renderer.View);

if (_childView is Layout layout)
{
layout.LayoutChanged += OnLayoutChanged;
}
}
}

void OnLayoutChanged(object sender, EventArgs e)
{
RequestLayout();
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Expand Down

0 comments on commit 9e47f08

Please sign in to comment.