How to disable scrolling in ListView?

12,749

Solution 1

I also face the same problem,I solved it by using Bindable StackLayout inside Scroll View.

<ScrollView>
    <StackLayout BindableLayout.ItemsSource="{Binding ItemSource}">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                 <Label Text="{Binding Name}" />
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>
</ScrollView>

Solution 2

Listview already has an inbuilt scroll mechanism, you should never try to put ListView inside ScrollView.

ListView uses UI Virtualization, which means, it will only create required items in the visible view, and not all the items. For example, if you load 240 countries, it will only actually create 20 countries and as you scroll, it will create more or reuse existing items.

By putting ListView inside ScrollView, you will disable UI virtualization.

Infact, Xamarin.Forms does not do anything special for scrolling, it is feature of underlying platform Android/iOS/Windows that provide UI virtualization to improve speed.

Share:
12,749
Piotrek
Author by

Piotrek

Updated on June 04, 2022

Comments

  • Piotrek
    Piotrek about 2 years

    Xamarin.Forms ListView has some tendency to stop stretching after some maximal height. When I put too many items in ListView, it creates scrollbar and height of it doesn't increase while I'm adding more of them.

    It could be confusing to user if <ListView> with scrollbar is inside <ScrollView> object that has scrollbar too. Layout bahaves unnaturally, forcing user to scroll to the end of listview to be able to continue scrolling scrollView.

    So, summing up:

    How can I disable scrollbar in ListView, forcing it to have the height of all its interior children?

    Children don't have equal heights. Also, they can change during app lifetime.

    <ScrollView>
        <StackLayout>
            <!-- some buttons and labels -->
            <ListView ItemsSource="{Binding Data}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Padding="20">
                                <!-- Some information -->
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ScrollView>
    

    Platform on which I test my app: Windows 10 UWP.