Xamarin.Forms - ListView With Pull To Refresh

Introduction
 
 
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files are the most easily done tasks using native file APIs on each platform. Alternatively, embedded resources are also a simpler solution to distribute the data files with an app.

Pull to Refresh

Xamarin Forms ListView control has the ability to allow the user to pull down from the top of the ListView to trigger a refresh command. When the user triggers a PullToRefresh the Command will be invoked the Refreshed event.

Prerequisites
  • Visual Studio 2017 (Windows or Mac)
Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself.

Choose the Cross-platform App project under Visual C#-->Cross-platform in the New Project dialog.
 

Now Select the Blank App and Choose Portable Class Library(PCL).
 
 
 
Subsequently, go to the solution. In there, you get all the files and sources of your project (PCL). Now, select XAML page and double-click to open the MainPage.Xaml page.

You now have a basic Xamarin.Forms app. Click the Play button to try it out.
 
 
Setting up the User Interface

In this step create a simple ListView.

Now, write the following code .
 
MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2.    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.    xmlns:local="clr-namespace:XamarinListView"  
  5.    x:Class="XamarinListView.MainPage">  
  6.    <ContentPage.Content>  
  7.       <StackLayout>  
  8.          <Image x:Name="imgBanner"></Image>  
  9.          <ListView x:Name="listPlatforms" ></ListView>  
  10.       </StackLayout>  
  11.    </ContentPage.Content>  
  12. </ContentPage>  
Now, add a list item to ListView. and write the following code .
 
 
MainPage.xaml.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7.   
  8. namespace XamarinListView  
  9.    {  
  10.    public partial class MainPage : ContentPage  
  11.    {  
  12.    List<string> listItems = new List<string>();  
  13.    public MainPage()  
  14.     {  
  15.       InitializeComponent();  
  16.       imgBanner.Source = ImageSource.FromResource("XamarinListView.images.banner.png");  
  17.       listItems.Add("Xamarin.Forms");  
  18.       listItems.Add("Xamarin.Android");  
  19.       listItems.Add("Xamarin.iOS");  
  20.       listPlatforms.ItemsSource = listItems;  
  21.       }  
  22.    }  
  23. }  
Click the Play button to try it out.
 
Implement Pull to Refresh

In this step you need to implement the Pull to Refresh in MainPage.Xaml.

IsPullToRefreshEnabled="True"
 
Now, write the following code .

MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.    xmlns:local="clr-namespace:XamarinListView"  
  5.    x:Class="XamarinListView.MainPage">  
  6.    <ContentPage.Content>  
  7.       <StackLayout>  
  8.          <Image x:Name="imgBanner"></Image>  
  9.          <ListView x:Name="listPlatforms" IsPullToRefreshEnabled="True"></ListView>  
  10.       </StackLayout>  
  11.    </ContentPage.Content>  
  12. </ContentPage>  
In this step, add the RefreshCommand to ListView
  1. Command RefreshCommand = new Command(); 
Now, write the code given below.
 
MainPage.xaml.cs
  1. listPlatforms.RefreshCommand = new Command(() => {  
  2.     //Do your stuff.    
  3.     RefreshData();  
  4.     listPlatforms.IsRefreshing = false;  
  5. });  
Full Code - MainPage.Xaml.cs
 
 
MainPage.Xaml.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7. namespace XamarinListView {  
  8.     public partial class MainPage: ContentPage {  
  9.         List < string > listItems = new List < string > ();  
  10.         public MainPage() {  
  11.             InitializeComponent();  
  12.             imgBanner.Source = ImageSource.FromResource("XamarinListView.images.banner.png");  
  13.             listItems.Add("Xamarin.Forms");  
  14.             listItems.Add("Xamarin.Android");  
  15.             listItems.Add("Xamarin.iOS");  
  16.             listPlatforms.ItemsSource = listItems;  
  17.             listPlatforms.RefreshCommand = new Command(() => {  
  18.                 //Do your stuff.    
  19.                 RefreshData();  
  20.                 listPlatforms.IsRefreshing = false;  
  21.             });  
  22.         }  
  23.         public void RefreshData() {  
  24.             listItems.Add("Xamarin Monkeys");  
  25.             listPlatforms.ItemsSource = null;  
  26.             listPlatforms.ItemsSource = listItems;  
  27.         }  
  28.     }  
  29. }  
 
 
Click the Play button to try it out.
 
 
 
I hope you have understood how to use ListView with Pull to Refresh in Xamarin.forms.

Thanks for reading. Please share comments and feedback.


Similar Articles