Brandon Church
Brandon Church

Reputation: 177

Xamarin Forms ScrollView Jitters When Descendents are Added

I'm new to Xamarin, so hopefully this is an easy fix. The problem is simple: I have a ScrollView containing a StackLayout, and adding new children to the StackLayout briefly causes the existing children to squish together causing a jittery visual disturbance. In the application I'm writing, children are being added to the StackLayout as the user scrolls down, so this jitter is also causing some more complicated issues with my scroll events.

The isolated issue is easy to reproduce:

<ScrollView>
    <StackLayout>
        <Button Clicked="Button_Clicked" Text="Click Me" HeightRequest="80"/>
        <StackLayout x:Name="Column1"/>
    </StackLayout>
</ScrollView>
private void Button_Clicked(object sender, EventArgs e)
{
    for (int i = 0; i < 5; i++)
        Column1.Children.Add(new Frame() { BackgroundColor = Xamarin.Forms.Color.Blue, HeightRequest = 100 });
}

Is there any way to create a scrollable view and add content dynamically without disrupting existing content? Any help would be much appreciated!

Upvotes: 1

Views: 417

Answers (1)

Cheesebaron
Cheesebaron

Reputation: 24460

It would be much more appropriate to use a view such as a CollectionView or ListView to add children to a scrolling view on the fly. They are both more efficient in terms of only loading what is visible on the screen, and already have built in logic for adding/removing children such that the layout behaves nicely.

With CollectionView/ListView you can still have children that look differently, by using a TemplatSelector.

Upvotes: 2

Related Questions