Functionality Of A ListView In Xamarin.Forms

In this article, we will discuss three different functionalities of ListView.

  • Pull to Refresh
  • Selection
  • Context Action

Pull To Refresh

Xamarin provides pull to refresh functionality on all platforms. In this functionality, you will pull your list view down to refresh it. While pulling it down, an activity indicator is shown that can read all of your written code and show the list again.

In an actual example, all the data comes from a remote API or from some other source and refreshes your data. But in this example, we can hard code our list data and on refreshing the list, we can add some more data in it.

Let’s implement it.


  1. <ListView x:Name="lst" IsPullToRefreshEnabled="True" Refreshing="lst_Refreshing">  
  2. </ListView>   

You can also make item template, data template, or cells in your list. But for now, I just made a simple List View to understand its functionality.


  1. public List()  
  2. {  
  3.     InitializeComponent();  
  4.     lst.ItemsSource = new List<string>() { "Item 1" , "Item 2" };
  5. }
  7. private void lst_Refreshing(object sender, EventArgs e)  
  8. {  
  9.     lst.ItemsSource = new List<string>() { "Item 1""Item 2" , "Item 3""Item 4"};  
  10.     lst.IsRefreshing = false;
  11. }

When the list is initialized, it contains 2 items and after refreshing, it can add 2 more items in it.

Output on Android
Pull To Refresh

Output - When List is refreshed, you will see an activity indicator.

Pull To Refresh

Output - After refreshing a list.
Pull To Refresh


You can also handle an event when list item is selected. You can move to the next page. Now, any alert or anything you want can be done by this event.

In our example, when user selects a list item, we will show him a display box that contains a message of selected item name in it.

Let’s Implement it.


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


  1. public List()  
  2. {  
  3.    InitializeComponent();  
  4.    lst.ItemsSource = new List<string>() { "Item 1" , "Item 2" };
  5. }
  7. private void lst_ItemSelected(object sender, SelectedItemChangedEventArgs e)  
  8. {  
  9.    DisplayAlert("Item Selected",e.SelectedItem.ToString(),"ok");  
  10. }   

In selected item event, we can show a display box that contains selected item name.


Xamarin ListView selection

Context Action

You can set different context actions in Xamarin.Forms. Context Actions can be shown -

On iOS: Swipe it from right to left.

On Android: when the user performs a long-click (press and hold an item).

You can do any action on it. In this example, we are going to make one context item named “More” and when the More is tapped, the Display box is shown that contains its name.



  1. <ListView x:Name="lst" ItemSelected="lst_ItemSelected">  
  2.       <ListView.ItemTemplate>  
  3.           <DataTemplate>  
  4.               <ViewCell>  
  5.                   <StackLayout>  
  6.                       <Label Text="{Binding .}"></Label>  
  7.                   </StackLayout>  
  8.                   <ViewCell.ContextActions>  
  9.                       <MenuItem Text="More" x:Name="More" Clicked="More_Clicked" CommandParameter="{Binding .}"></MenuItem>  
  10.                   </ViewCell.ContextActions>  
  11.               </ViewCell>  
  12.           </DataTemplate>  
  13.       </ListView.ItemTemplate>  
  14.   </ListView>   

Here you see that we set a context action to view cell. You may also set context actions to Image cell or Text Cell.


  1. List<string> templist = new List<string>() { "Item 1""Item 2""Item 3""Item 4"};  
  3. public List()  
  4. {  
  5.     InitializeComponent();  
  6.     lst.ItemsSource = templist;  
  7. }  
  9. private void More_Clicked(object sender, EventArgs e)  
  10. {  
  11.     var name = (sender as MenuItem).CommandParameter as string;  
  12.     DisplayAlert("Context Action", name, "ok");  
  13. }   

Output -  Press and hold an item

Xamarin.Forms context actions

Output - When tapped on more

Xamarin.Forms context actions

That's it. Thanks for reading.

Similar Articles