Reputation: 13
Basically I'm trying to dinamically add layouts to the content of a scrollView. I did an example code but it only works at the second click, for some reason it doesn't render the first layout added!!! Am I doing something wrong?
public class PageExperiment
{
ScrollView _page;
public PageExperiment()
{
_page = new ScrollView { Orientation = ScrollOrientation.Vertical,
Content = new RelativeLayout { BackgroundColor = Color.White} };
var button = new Button { Text="Add Button"};
button.Clicked += OnButtonClicked;
((RelativeLayout)_page.Content).Children.Add(button,xConstraint:null);
}
public ScrollView getPage()
{
return _page;
}
void OnButtonClicked(object sender, EventArgs e)
{
var relative = AddLayout();
var content = (RelativeLayout)_page.Content;
var v = content.Children[content.Children.Count - 1];
Constraint xConstraint = Constraint.Constant(0);
Constraint yConstraint = Constraint.RelativeToView(v, (parent, sibling) => {
return sibling.Y + sibling.Height;
});
((RelativeLayout)_page.Content).Children.Add((View)relative, xConstraint, yConstraint);
}
public VisualElement AddLayout()
{
var root = new RelativeLayout();
root.BackgroundColor = Color.Red;
var a = new Label { Text = "Button Added!!!!", BackgroundColor = Color.Purple};
a.FontSize = 30;
root.Children.Add(a, xConstraint:null);
return root;
}
}
Here is the main:
public class App : Application
{
public App()
{
var root = new PageExperiment();
MainPage = new ContentPage
{
Content = root.getPage()
};
}
}
Upvotes: 0
Views: 1151
Reputation: 1196
Add
((RelativeLayout)_page.Content).ForceLayout ();
to the end of your OnButtonClicked(object sender, EventArgs e)
Looks like this is a bug in the relative Layout rendering. It doesn't appear to be an issue with the scroll view. I see you've already placed a Bug Report with Xamarin that's likely all the more that can be done to "fix" this. As I noted on the bug report. The bugs not just for layouts within layouts its any visual element added into a relative layout on the first click. Adding a call to ForceLayout should make your app functional though there might be a performance hit.
Upvotes: 1