To demonstrate its behavior, we create a listView with an ItemsSource given by a custom method CreateList. We’ll discuss its implementation shortly. To enable the pull-to-refresh interaction, we set IsPullToRefreshEnabled to true.
var listView = new ListView {
ItemsSource = CreateList(),
IsPullToRefreshEnabled = true,
};
Now we need to react, when the user pulled to refresh the list. Therefore, we subscribe to the list view’s Refreshing event. First, we update the ItemsSource with another call to CreateList. Afterwards, we call EndRefresh to return to the normal, non-freshing state. The list view will move back to the top of the page and the spinning wheel will disappear.
listView.Refreshing += (sender, e) => {
listView.ItemsSource = CreateList();
listView.EndRefresh();
};
The MainPage contains nothing but the listView.
MainPage = new ContentPage {
Padding = new Thickness(0, Device.OS == TargetPlatform.iOS ? 20 : 0, 0, 0),
Content = listView,
};
The CreateList method returns the current date and time in various formats. It selects each of 19 format specifiers and concatenates it with the current DateTime in the respective format. (Because we are calling DateTime.Now multiple times, their values – especially the milliseconds – might differ.)
static List<string> CreateList()
{
var specifiers = new [] {
"d", "D", "f", "F", "g", "G", "M", "m", "O", "o", "R", "r", "s", "t", "T", "u", "U", "y", "Y",
};
return specifiers.Select(format => format + ": " + DateTime.Now.ToString(format)).ToList();
}
The following screenshots show the outcome of this method as well as the update interaction via pull-to-refresh.