xamarinscrollviewxamarin.formscontent-pages

Horizontal ScrollView - xamarin.forms


I know there are many about this topic (Scroll horizontally in Xamarin.Forms ScrollView), but I could not implement the horizontal scrollview which scrolls horizontally.

public class DetailView : ContentPage
{
    public DetailView ()
    {
        StackLayout stack = new StackLayout {
            Orientation = StackOrientation.Horizontal,
            };
        for (int i = 0; i < 40; i++)
            stack.Children.Add (new Button { Text = "Button" });
        var scrollView = new ScrollView
        {
            Orientation = ScrollOrientation.Horizontal,
            Content = stack
        };
        Content = scrollView;
    }   
}

Any ideas?


Solution

  • try this:

    public DetailView()
    {
        var scrollableContent = new StackLayout()
        {
            Orientation = StackOrientation.Horizontal,
            HorizontalOptions = LayoutOptions.Fill,
            Children =
            {
                new BoxView(){HeightRequest=40, WidthRequest=40, BackgroundColor = Color.Red},
                new BoxView(){HeightRequest=40, WidthRequest=40, BackgroundColor = Color.Green},
                new BoxView(){HeightRequest=40, WidthRequest=40, BackgroundColor = Color.Blue},
                new BoxView(){HeightRequest=40, WidthRequest=40, BackgroundColor = Color.Maroon},
            }
        };
    
        Content = new ScrollView()
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            Orientation = ScrollOrientation.Horizontal,
            Content = scrollableContent,
        };
    }