Practical Guide For ListView Cells In Xamarin.Forms

In this article, you will learn about cells used in ListView in Xamarin.Forms.

This article will cover the following things about List View.

  1. Introduction to ListView
  2. Create a Basic ListView
  3. Binding & Cells
    • Text Cell
    • Image Cell
    • View Cell

Target Audience

People with basic knowledge of C# and XAML.

Introduction to ListView

ListView is used to present data in list, especially long lists that require scrolling.

Xamarin

You all are already familiar with list as you are using list of contacts daily in your mobile phone. Other examples include your messaging inbox that contains a list of messages. Let’s implement this.

Create a Basic ListView

In our mobile application, we can implement list by gathering data from remote API or by some database. But in this example, we will make a sample hard code list of data and implement it. Now, start creating a basic ListView. The necessary code is given.

XAML

  1. <ListView x:Name="lst"></ListView>   

 Code

  1. lst.ItemsSource = new List<string>() { "item 1""item 2""Item 3" };  

Output on Windows and Android

Xamarin

Binding & Cells

Now, make a complete list and bind its elements. Firstly, we make a class and then bind its properties with ListView.

Add a new class.

  1. public class Contacts {  
  2.     public string Name {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Num {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string imgsource {  
  11.         get;  
  12.         set;  
  13.     }  
  14. }   

For just practicing purposes, make a class of contacts and define three properties of Name, Num, and imgsource as shown above in the code.

Then, go to code file, add some data, and change item source of ListView. 

  1. lst.ItemsSource = new List < Contacts > () {  
  2.     new Contacts() {  
  3.             Name = "Umair", Num = "0456445450945", imgsource = "http://bit.ly/2oDQpPT",  
  4.         },  
  5.         new Contacts() {  
  6.             Name = "Cat", Num = "034456445905", imgsource = "http://gtty.im/2psFEos",  
  7.         },  
  8.         new Contacts() {  
  9.             Name = "Nature", Num = "56445905", imgsource = "http://gtty.im/2psFEos",  
  10.         },  
  11. };   

As we discussed earlier, there are three types of cells in ListView. Now, let's discuss these three cells and perform some data binding.

TextCell

TextCell is the data template of ListView. In this cell, we can set text and details. Other properties of TextCell include text color, detail color etc.

XAML 

  1. <ListView x:Name="lst">  
  2.     <ListView.ItemTemplate>  
  3.         <DataTemplate>  
  4.             <TextCell Text="{Binding Name}" Detail="{Binding Num}"></TextCell>  
  5.         </DataTemplate>  
  6.     </ListView.ItemTemplate>  
  7. </ListView>   

Here, you can see that we used the TextCell and set binding. We set Binding of ‘Text’ with ‘Name’ and we binded the ‘detail’ with ‘Num’ property of contact class.

Output on Android and Windows

Xamarin

This is how TextCell is shown on Android and Windows. It will show some text and its details.

ImageCell

Now, let's use an ImageCell. ImageCell is a cell that contains images in it. ImageCell includes ImageSource, Text, and Details.

XAML 

  1. <ListView x:Name="lst">  
  2.     <ListView.ItemTemplate>  
  3.         <DataTemplate>  
  4.             <ImageCell Text="{Binding Name}" Detail="{Binding Num}" ImageSource="{Binding imgsource}"></ImageCell>  
  5.         </DataTemplate>  
  6.     </ListView.ItemTemplate>  
  7. </ListView>   

Here, you can see that we used the ImageCell and set Binding. We set Binding of ‘Text’ with ‘Name’, ‘detail’ with ‘Num’ and ‘ImageSource’ with ‘imgsource’ property of contact class.

Output on Android and Windows

Xamarin

This template looks cool on all mobile phone devices. But the List looks weird on a Windows desktop and there is no way to set image height in ImageCell. You have to make your own custom cell to overcome this problem. Let’s discuss how to make a custom cell (ViewCell). Read the points blow.

ViewCell

ViewCell is the third cell that we use in list. In ViewCell, we can make a custom View of list. That is the View made according to our desire. Let’s go through its example.

XAML 

  1. <ListView x:Name="lst" HasUnevenRows="True">  
  2.     <ListView.ItemTemplate>  
  3.         <DataTemplate>  
  4.             <ViewCell>  
  5.                 <StackLayout Orientation="Horizontal">  
  6.                     <StackLayout Orientation="Vertical">  
  7.                         <Label Text="{Binding Name}" Font="18"></Label>  
  8.                         <Label Text="{Binding Num}" TextColor="Gray"></Label> </StackLayout>  
  9.                     <Image Source="{Binding imgsource}" HeightRequest="30" WidthRequest="50" HorizontalOptions="EndAndExpand"></Image>  
  10.                 </StackLayout>  
  11.             </ViewCell>  
  12.         </DataTemplate>  
  13.     </ListView.ItemTemplate>  
  14. </ListView>   

Here, you can see that we made two stack layouts with different orientation to make our ViewCell different. We can also set its color, height, width, and other options to make it better. 

Output on Android and Windows

Xamarin

Now, give it a try and make your own custom ViewCell.

Thanks for reading.