Xamarin.Forms - ListView Header And Footer Template

Introduction

In this article, you will learn about Header Template List View in Xamarin.Forms applications. Most list views do not have header templates. But if we are using lists often then we can use this header template to display the content. So in this article we will learn about header and footer templates and list view Items.

Pre-requisites

  • Windows 10 (Recommended)
  • Visual Studio 2017 Community Edition (It is a free software available online).

Let’s start!

Step 1

Open Visual Studio->New Project->Templates->Visual C#->Cross Platform->Cross Platform App(Xamarin). Now, give the project name and project location.

 

 

Step 2

Next, open a new window. Here, select Template->Blank App, UI Technology->Xamarin.Forms and finally Code Sharing Strategy ->Portable Class Library (PCL).

 

 

Step 3

Open Solution Explorer->Project Name (PCL)-> MainPage.xaml then click to open XAML Code .

Here, put in the list view code.

 

 

XAML Code

  1. <ListView x:Name=”MainListView” Header=”” HasUnevenRows=”True”>  
  2.     <ListView.HeaderTemplate>  
  3.         <DataTemplate>  
  4.             <Grid>  
  5.                 <Label Text=”The Header Template” FontSize=”30″ BackgroundColor=”Violet” TextColor=”White”></Label> </Grid>  
  6.         </DataTemplate>  
  7.     </ListView.HeaderTemplate>  
  8.     <ListView.FooterTemplate>  
  9.         <DataTemplate>  
  10.             <Grid>  
  11.                 <Label Text=”The Footer Template” FontSize=”30″ BackgroundColor=”Blue” TextColor=”White”></Label> </Grid>  
  12.         </DataTemplate>  
  13.     </ListView.FooterTemplate>  
  14.     <ListView.ItemTemplate>  
  15.         <DataTemplate>  
  16.             <ViewCell>  
  17.                 <Grid Padding=”12″>  
  18.                     <Label Text=”{Binding .}” FontSize=”Medium”></Label> </Grid>  
  19.             </ViewCell>  
  20.         </DataTemplate>  
  21.     </ListView.ItemTemplate>  
  22. </ListView>  

Step 4

Open Solution Explorer->Project Name (PCL)-> MainPage.xaml cs then click to open cs code then create list view values.

 

 

C# Code

  1. MainListView.ItemsSource = new List < string > {  
  2.     "Anbu",  
  3.     "Guna",  
  4.     "Hari",  
  5.     "Mani",  
  6.     "Karthi",  
  7.     "Sathis",  
  8.     "Poovarasan",  
  9.     "Mukesh",  
  10.     "Karthik"  
  11. };  
Step 5

Next create Click Event in ListView below the MainPage(),

 

 

C# Code

  1. public MainPage() {  
  2.     InitializeComponent();  
  3.     MainListView.ItemsSource = new List < string > {  
  4.         "Anbu",  
  5.         "Guna",  
  6.         "Hari",  
  7.         "Mani",  
  8.         "Karthi",  
  9.         "Sathis",  
  10.         "Poovarasan",  
  11.         "Mukesh",  
  12.         "Karthik"  
  13.     };  
  14.     MainListView.ItemSelected += MainListView_ItemSelected;  
  15. }  
  16. private void MainListView_ItemSelected(object sender, SelectedItemChangedEventArgs e) {  
  17.     DisplayAlert("This is " + e.SelectedItem.ToString(), """Ok");  
  18. }  
Step 6

Finally set up Default Running Platform [Android /iOS/UWP].

iOS

 
Android 

UWP

 
Finally , we have successfully created Xamarin.forms ListView Header & Footer template 

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now