Created
February 5, 2016 13:25
-
-
Save dkudelko/b8d37d21b2a8ae3e8e14 to your computer and use it in GitHub Desktop.
Load More Items at End of ListView in Xamarin.Forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Xamarin.Forms; | |
using System.Collections.ObjectModel; | |
using System.Threading.Tasks; | |
namespace LoadMoreBottom | |
{ | |
public class App : Application | |
{ | |
ObservableCollection<string> Items; | |
bool isLoading; | |
Page page; | |
public App () | |
{ | |
Items = new ObservableCollection<string> (); | |
var listview = new ListView (); | |
listview.ItemsSource = Items; | |
listview.ItemAppearing += (sender, e) => | |
{ | |
if(isLoading || Items.Count == 0) | |
return; | |
//hit bottom! | |
if(e.Item.ToString() == Items[Items.Count - 1]) | |
{ | |
LoadItems(); | |
} | |
}; | |
// The root page of your application | |
page = new ContentPage { | |
Content = new StackLayout { | |
VerticalOptions = LayoutOptions.Center, | |
Children = { | |
listview | |
} | |
} | |
}; | |
MainPage = new NavigationPage (page); | |
LoadItems (); | |
} | |
private async Task LoadItems() | |
{ | |
isLoading = true; | |
page.Title = "Loading"; | |
//simulator delayed load | |
Device.StartTimer (TimeSpan.FromSeconds (2), () => { | |
for (int i = 0; i < 20; i++) { | |
Items.Add (string.Format("Item {0}", Items.Count)); | |
} | |
page.Title = "Done"; | |
isLoading = false; | |
//stop timer | |
return false; | |
}); | |
} | |
protected override void OnStart () | |
{ | |
// Handle when your app starts | |
} | |
protected override void OnSleep () | |
{ | |
// Handle when your app sleeps | |
} | |
protected override void OnResume () | |
{ | |
// Handle when your app resumes | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment