Forum Xamarin.Forms

Striped listview - performance

MikilllMikilll Member ✭✭✭

Hello,
I created a ListView with alternating row colors. There are many tutorials over the Internet how to do that, i.e. here:

https://forums.xamarin.com/discussion/27896/listview-alternating-row-background-colors

Unfortunately, the performance is quite bad when the number of items grows. For normal Listview, we can use caching strategy "Recycle Element" to have a nice performance. Unfortunately, it does not work for striped Listviews because cells are not identical. So how to have a nice performance in striped ListViews?

Answers

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    @Mikilll

    There a lots solution in the link you provide. Which are you using?
    There are several ways to improving listview performance:

    • Bind the ItemsSource property to an IList collection instead of an IEnumerable collection, because IEnumerable collections don't support random access.
    • Use the built-in cells (like TextCell / SwitchCell ) instead of ViewCell whenever you can.
    • Use fewer elements. For example consider using a single FormattedString label instead of multiple labels.
    • Replace the ListView with a TableView when displaying non-homogenous data – that is, data of different types.
    • Limit the use of the Cell.ForceUpdateSize method. If overused, it will degrade performance.
    • On Android, avoid setting a ListView's row separator visibility or color after it has been instantiated, as it results in a large performance penalty.
    • Avoid changing the cell layout based on the BindingContext. This incurs large layout and initialization costs.
    • Avoid deeply nested layout hierarchies. Use AbsoluteLayout or Grid to help reduce nesting.
    • Avoid specific LayoutOptions other than Fill (Fill is the cheapest to compute).
    • Avoid placing a ListView inside a ScrollView for the following reasons:
    • The ListView implements its own scrolling.
    • The ListView will not receive any gestures, as they will be handled by the parent ScrollView.
    • The ListView can present a customized header and footer that scrolls with the elements of the list, potentially offering the functionality that the ScrollView was used for. For more information see Headers and Footers.
    • Consider a custom renderer if you need a very specific, complex design presented in your cells.

    Please check the following link for more information:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/performance#improving-listview-performance

  • chetanrawatchetanrawat USMember ✭✭✭

    Hi Mikill you can Manage this things with binding a background color with property at run time using ObservableCollection.

    if (data!= null && data.Count > 0)
    {
     data.Select((value, index) => new { value, index }).Where(x => x.index % 2 == 0).Select(x => { x.value.BackgroundColor = "#ecf0f5"; return x; }).Select(x => x.value).ToList();
    }
    

    private string _BackgroundColor = "#ffffff";

                    public string BackgroundColor
                    {
                        get { return _BackgroundColor; }
                        set { _BackgroundColor = value; this.OnPropertyChanged("BackgroundColor"); }
                    }
    
     <ViewCell>
                                    <Grid
                                        Margin="0"
                                        Padding="10,5"
                                        BackgroundColor="{Binding BackgroundColor}"
                                        HorizontalOptions="FillAndExpand"
                                        VerticalOptions="FillAndExpand">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="auto" />
                                        </Grid.RowDefinitions>
    </Grid>
    </ViewCell>
    
Sign In or Register to comment.