Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable scrolling in ListView?

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.

like image 611
Piotrek Avatar asked Dec 03 '22 14:12

Piotrek


2 Answers

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>
like image 64
Pragnesh Mistry Avatar answered Dec 15 '22 13:12

Pragnesh Mistry


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.

like image 25
Akash Kava Avatar answered Dec 15 '22 15:12

Akash Kava