Adding "Pull To Refresh" To Your List In Xamarin.Forms

Targeted Audience

People with a basic knowledge of C# and Xamarin.

Tools

Visual Studio with Xamarin installed.

It is necessary to add "Pull to Refresh" to your lists when you are creating a list with a huge number of data because if you add or remove anything from the list, you will see the changes by refreshing it. So, this component is necessary for the ListView.

Let’s see how to achieve this functionality on a “Contact List” in simple steps.

Pull To Refresh

For the start, we have to create a ListView first for this purpose and have to insert some data into it. Add a class in your project with the name “Contacts” and create two properties “Name” and “Address” in it.

C# Code
  1. public class Contacts  
  2.     {         
  3.         private string name;  
  4.         public string Name  
  5.         {  
  6.             get { return name; }  
  7.             set { name = value; }  
  8.         }  
  9.         private string address;  
  10.         public string Address  
  11.         {  
  12.             get { return address; }  
  13.             set { address = value; }  
  14.         }  
  15.     }  

After that, in your XAML file, create a ListView. If you are not familiar with creating ListView, you can create it using the code given below.

XAML Code
  1. <ContentPage   
  2.              xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:PracApp1"  
  5.              x:Class="PracApp1.MainPage"   
  6.              x:Name="ContentPage1">  
  7.     <ListView x:Name="listView" IsPullToRefreshEnabled="True" Refreshing="ListView_Refreshing">  
  8.         <ListView.ItemTemplate>  
  9.             <DataTemplate>  
  10.                 <TextCell Text="{Binding Name}" Detail="{Binding Address}"></TextCell>  
  11.             </DataTemplate>  
  12.         </ListView.ItemTemplate>  
  13.     </ListView>  
  14. </ContentPage>  

I have added two things here in the ListView - “IsPullToRefreshEnabled” allows enabling or disabling the refreshing; and “Refreshing” is basically the method which is called when the list is pulled to refresh.

Now, in the xaml.cs file, all we need is just to bind the list with the data and create the method for refreshing. You can use the code below.

C# Code
  1. List<Contacts> GetContacts()  
  2.         {  
  3.             return new List<Contacts> {  
  4.                 new Contacts { Name = "Mosh", Address = "" },  
  5.                 new Contacts { Name = "John", Address = "Hey, Lets Talk!" }  
  6.                };  
  7.         }  
  8.         private void ListView_Refreshing(object sender, EventArgs e)  
  9.         {  
  10.             listView.ItemsSource = GetContacts();  
  11.             listView.EndRefresh();  
  12.         }  
  13.         public MainPage()  
  14.         {  
  15.             InitializeComponent();  
  16.             listView.ItemsSource = GetContacts();  
  17.             this.BindingContext = this;  
  18.         }  

What I have done here is that below IntializeComponent, I bind the item source of the list with a method which returns the list of two objects. Below that method, there is another method which will be called while refreshing. It also sets the list’s item source with the new contacts list. Also, you have to end refreshing as I did, otherwise the activity indicator will not disappear.

The result will be something like this on the iPhone device.

Adding Pull To Refresh To Your List In Xamarin.Forms

This article offered you a brief introduction of how to add "Pull to Refresh" to your list. Remember that the data I put here is just for example purposes. You can bind your list’s item source with the dynamic data. This is just for learning purposes.


Similar Articles