Articles in this section
Category / Section

How to change SpanCount for GridLayout based on screen size and orientation in .NET MAUI ListView (SfListView)?

3 mins read

In the .NET MAUI ListView (SfListView), the GridLayout arranges items into columns according to the SpanCount property. You can dynamically adjust the SpanCount based on the application’s view size and orientation (portrait or landscape).

XAML
Initialize the ItemsLayout as GridLayout, by default set the SpanCount to “2”

<syncfusion:SfListView x:Name="listView" SelectionMode="Multiple" ItemSource="{Binding GalleryInfo}" 
                       Grid.Row="1" ItemSize="{OnIdiom Phone='150', Tablet='150', Desktop='170'}" ItemSpacing="3">
   <syncfusion:SfListView.ItemsLayout>
       <syncfusion:GridLayout SpanCount="2"/>
   </syncfusion:SfListView.ItemsLayout>
</syncfusion:SfListView>

C#
Implement a behavior to dynamically customize the SpanCount based on the view size of the application by leveraging the PropertyChanged method to capture width changes.

public class GridSpanBehavior : Behavior<ContentPage>
{
   SfListView listView;

   protected override void OnAttachedTo(ContentPage bindable)
   {
       listView = bindable.FindByName<SfListView>("listView");
       bindable.PropertyChanged += Bindable_PropertyChanged;
       base.OnAttachedTo(bindable);
   }

   private void Bindable_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
   {
       if (e.PropertyName == "Width" || e.PropertyName == "Height")
       {
           var size = Application.Current.MainPage.Width / listView.ItemSize;

           if (DeviceInfo.Platform == DevicePlatform.Android || DeviceInfo.Platform == DevicePlatform.iOS)
           {
               (listView.ItemsLayout as GridLayout).SpanCount = DeviceInfo.Idiom == DeviceIdiom.Phone ? 2 : 4;
           }
           else if (DeviceInfo.Platform == DevicePlatform.WinUI)
           {
               (listView.ItemsLayout as GridLayout).SpanCount = DeviceInfo.Idiom == DeviceIdiom.Desktop ? 4 : 2;
               listView.ItemSize = DeviceInfo.Idiom == DeviceIdiom.Desktop || DeviceInfo.Idiom == DeviceIdiom.Tablet ? 230 : 140;
           }

           (listView.ItemsLayout as GridLayout).SpanCount = (int)size;
       }
   }

   protected override void OnDetachingFrom(ContentPage bindable)
   {
       bindable.PropertyChanged -= Bindable_PropertyChanged;
       listView = null;
       base.OnDetachingFrom(bindable);
   }
}
  • The SpanCount is dynamically adjusted by calculating the screen width relative to the ItemSize. For instance, if the screen width is 300 and the ItemSize is 150 in portrait mode, the SpanCount would be set to 2.
  • The Bindable_PropertyChanged method recalculates and sets the SpanCount whenever the screen Width or Height changes, adapting to changes in orientation and platform type.

Output

Vertical-orientation.png

Horizontal-Orientation.png

Download the complete sample from GitHub

Conclusion
I hope you enjoyed learning how to change SpanCount based on screen size and orientation in .NET MAUI ListView.
You can refer to our .NET MAUI ListView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. Explore our .NET MAUI ListView example to understand how to create and manipulate data.
Check out our components from the License and Downloads page for current customers. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied