ListView is slow to load, any performance guidelines?

Hi I am using a basic Listview in forms with hard coded data in a list. When navigating to the page which contains the listview, it takes a nice 1-2 seconds to load. Any specific reason why this is happening?

Code:

protected override void OnAppearing ()
        {
            base.OnAppearing ();
            //Get Menuitems for current menu
            MenuListViewModel.MenuItems= Task.Run (async () => await UCommerceServices.GetMenuItemsForCategory(MenuListViewModel.ParentId)).Result;
            foreach (var menuItem in MenuListViewModel.MenuItems) {
                menuItems.Add (menuItem);
            }
            activityIndicator.IsVisible = false;
        }

View:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Pizza4U.MenuListPage">
    <ContentPage.Content>
     <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
        <ListView x:Name="listView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout BackgroundColor="#eee" Orientation="Vertical">
                            <StackLayout Orientation="Horizontal">
                                <Image Source="{Binding ImageUrl}" />
                                <Label Text="{Binding Title}" TextColor="#f35e20" />
                                <Label Text="{Binding Description}" HorizontalOptions="EndAndExpand" TextColor="#503026" />
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
          <ActivityIndicator IsVisible="true" IsRunning="true"
            VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" x:Name="activityIndicator" />
        </Grid>
    </ContentPage.Content>
</ContentPage>

Note that the GetMenuItemsForCategory is retrieving a list which is currently hardcoded.

Thanks for your assistance.

Shaun

Tagged:

Best Answer

Answers

  • SpaceMonkeySpaceMonkey GBMember ✭✭✭

    ListView is rubbish.

    However, the new release could help:
    Try using ListViewCachingStrategy.RecycleElement as explained somewhere here http://forums.xamarin.com/discussion/56040/xamarin-forms-2-0-0-released/p1

    If that doesn't work, then use a custom renderer for the cells which is explained in the documentation. It explains how to write a custom renderer for the cells or for the whole ListView, it's better to write a renderer for the cells only.

    When I had to do this, I couldn't even write a custom renderer for the cells because of some bugs in Xamarin which I reported and have no idea if it was fixed or not:
    http://forums.xamarin.com/discussion/43051/strange-error-happens-when-saving-a-custom-renderer-object-to-a-java-object#latest
    http://forums.xamarin.com/discussion/42728/android-bug-1-4-3-6358-pre2-oncellpropertychanged-not-called-for-the-first-visible-cells#latest

    I really should have posted that to Bugzilla instead... But then, even that doesn't help sometimes like in the case of this horrible bug:
    https://bugzilla.xamarin.com/show_bug.cgi?id=35657
    which have been there for like 3 weeks now and no one gives a shit despite providing a sample project as requested.

    So, in my case I had to write a custom renderer for the layout that's used by the cells.

    Sorry for going off topic, I hope that helped.

  • NMackayNMackay GBInsider, University mod

    @ShaunGrech

    If your don't mind paying for custom controls then Telerik's RadListview has good performance and a lot of nice features that would be real a headache in the free version. We've moved to that and seen much better performance.

  • JulienRosenJulienRosen CAMember ✭✭✭✭

    how many items are you showing?

  • ShaunGrechShaunGrech USMember ✭✭

    I am replicating the code from the XamarinCRM app which I consider to be the gold standard. The images loaded in the XamarinCRM app are quite big (1600X1333). My images are similar (maybe slightly bigger in size). I have implemented an independent loader for the images, but when I navigate away from the list and navigate back, the list takes a nice 2 seconds to re-appear (even though the images have now been loaded already).

    I do not want to purchase external tools atm as I'm on a budget and I was under the impression that using a list is the recommended approach.

    Thanks for the help guys.

    Shaun

  • JulienRosenJulienRosen CAMember ✭✭✭✭

    The CRM app doesn't load more then a handful of items at a time. Again, how many items are you loading?

  • ShaunGrechShaunGrech USMember ✭✭

    At the moment I am loading around 6 items which is approximately the same as the CRM application. I realised that my images are slightly bigger than the CRM's. I also noticed that on Android I run into a memory exception stating that I am utilising too much memory.

  • ShaunGrechShaunGrech USMember ✭✭

    Is there a way how you can avoid running into a memory exception in Xamarin Forms? I am loading images from an API and sometimes the images might be a bit too big but it seems like I'm running into outofmemory exceptions way to often.

    Thanks guys,
    Shaun

  • JulienRosenJulienRosen CAMember ✭✭✭✭
    edited December 2015

    You really need to investigate the image sizes and scale them down to a resolution that makes sense for mobile. You should also attempt just removing the images temporarily and see if the issue persists. I am loading hundreds of items in a listview with no memory issues, and I'm sure you can see that the CRM app does not suffer from anything like that.

  • ShaunGrechShaunGrech USMember ✭✭

    Yes in fact I noticed that the images used in the XamarinCRM app are slightly smaller than mine. I am also running into a number of memory exceptions on Android simply because I am using a background image. I will look into some Image resize tools as I am retrieving my images through an API.

    Thanks for the help guys
    Shaun

Sign In or Register to comment.