Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll horizontally in Xamarin.Forms ScrollView

I am using Xamarin.Forms and have created a ScrollView, which contains a horizontal StackLayout. I want to be able to scroll horizontally, so I set:

Orientation  = ScrollOrientation.Horizontal;

But I don't get horizontal scroll. The content of the StackLayout is wider than the screen, and I see the content is being clipped at the edge.

How do I achieve horizontal scroll with Xamarin.Forms ?

like image 739
driis Avatar asked Jun 24 '14 14:06

driis


People also ask

How do I scroll with forms in xamarin?

Vertical indicates that the ScrollView will scroll vertically. This member is the default value of the Orientation property. Horizontal indicates that the ScrollView will scroll horizontally. Both indicates that the ScrollView will scroll horizontally and vertically.

How do I make StackLayout scrollable?

Scrolling content If a StackLayout contains too many children to display on a page, you can put the StackLayout in a ScrollView to allow scrolling. Set the Content property of ScrollView to the view you want to scroll. This is often a StackLayout , but it can be any view.

What is StackLayout in xamarin forms?

A StackLayout organizes child views in a one-dimensional stack, either horizontally or vertically. By default, a StackLayout is oriented vertically. In addition, a StackLayout can be used as a parent layout that contains other child layouts.

How do you stop scrolling in collection view xamarin forms?

Remove the scrollView and put the UI above the collection into the header of collectionView.


2 Answers

This is how I got it to work

      var scrollView = ScrollView
        {
            HorizontalOptions = LayoutOptions.Fill,
            Orientation = ScrollOrientation.Horizontal,

            Content = new StackLayout{
               Orientation = StackOrientation.Horizontal,
               Children = {}
            }
        };
like image 139
lee Avatar answered Oct 01 '22 11:10

lee


This nuget package will work:

https://github.com/SuavePirate/DynamicStackLayout

The property Words is a list of strings:

    <ScrollView Orientation="Horizontal" HorizontalOptions="FillAndExpand">
        <dynamicStackLayout:DynamicStackLayout ItemsSource="{Binding Words}" HorizontalOptions="Fill" Orientation="Horizontal" Padding="10, -0, 50, 10">
            <dynamicStackLayout:DynamicStackLayout.ItemTemplate>
                <DataTemplate>
                    <StackLayout BackgroundColor="Gray" WidthRequest="80" HeightRequest="80">
                        <Label Text="{Binding .}" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
                    </StackLayout>
                </DataTemplate>
            </dynamicStackLayout:DynamicStackLayout.ItemTemplate>
        </dynamicStackLayout:DynamicStackLayout>
    </ScrollView>

I hope it helps :)

like image 36
Ricardo de Assuncao Goncalves Avatar answered Oct 01 '22 13:10

Ricardo de Assuncao Goncalves