Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Dynamically adding items with labels to scrollview is extremely slow

$
0
0

So I have a horizontal scrollview that I'm trying to dynamically populate when the user takes a certain action. The items I am throwing into the view each contain 4 labels that are using custom fonts. When I try to add about 10 of these items it lags for about 1.5 seconds on android and 1 second on IOS. If I take the custom font out then its about 1 second on each platform. If I take out 3 of the labels and only display one then its almost instantaneous. Is there any known reason for the lag? And is there any way around it so I can still use a custom font without a huge lag?

Here's a quick sample I made that pretty much does what I'm doing in my app. However, my app has more stuff so the lag isn't quite as bad here but it is still very noticeable

public class App : Application
    {
        public int count;
        public ScrollView scroll, scroll2, scroll3;
        public App ()
        {
            count = 1;
            scroll = new ScrollView {
                VerticalOptions = LayoutOptions.Center,
                Orientation = ScrollOrientation.Horizontal
            };
            scroll2 = new ScrollView {
                VerticalOptions = LayoutOptions.Center,
                Orientation = ScrollOrientation.Horizontal
            };
            Button button = new Button(){
                Text = "click",
            };
            button.Clicked += (sender, e) => AddStuff();
            Button button2 = new Button(){
                Text = "click",
            };
            button2.Clicked += (sender, e) => AddStuff2();
            MainPage = new ContentPage {
                BackgroundColor = Color.White,
                Content = new StackLayout{
                    Children={
                        button,
                        scroll,
                        button2,
                        scroll2
                    }
                }
            };
        }
        //this one is instantaneous
        public void AddStuff()
        {
            StackLayout stack = new StackLayout () {
                Orientation = StackOrientation.Horizontal,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                HeightRequest = 200,
            };
            for (int i = 0; i < 11; i++)
                stack.Children.Add (
                    new StackLayout(){
                        Children = {
                            new Label (){TextColor = Color.Blue, Text = "Size: ", WidthRequest = 100 },
                        }
                    }
                );
            scroll.Content = stack;
            count++;
        }
        //this one takes forever
        public void AddStuff2()
        {
            StackLayout stack = new StackLayout () {
                Orientation = StackOrientation.Horizontal,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                HeightRequest = 200,
            };
            for (int i = 0; i < 11; i++)
                stack.Children.Add (
                    new StackLayout(){
                        Children = {
                            new Label (){TextColor = Color.Blue, Text = "Size: ", WidthRequest = 100 },
                            new Label (){TextColor = Color.Blue, Text ="" + count*i, WidthRequest = 100 },
                            new Label (){TextColor = Color.Blue, Text = "Size: ", WidthRequest = 100 },
                            new Label (){TextColor = Color.Blue, Text ="" + count*i, WidthRequest = 100 }
                        }
                    }
                );
            scroll2.Content = stack;
            count++;
        }
    }

and the custom font label for droid

[assembly: ExportRenderer (typeof (Label), typeof (CustomFontLabel_Droid))]
namespace df.Droid
{
    public class CustomFontLabel_Droid:LabelRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.Label> e) {
            base.OnElementChanged (e);
            var label = (TextView)Control;
            Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets, "SourceSansPro-Semibold.otf");
            label.Typeface = font;
        }
    }
}

Viewing all articles
Browse latest Browse all 77050

Trending Articles