Skip to content

Instantly share code, notes, and snippets.

@jamesmontemagno
Last active October 3, 2019 15:51
Show Gist options
  • Save jamesmontemagno/465ced24fc8a206dcaf3 to your computer and use it in GitHub Desktop.
Save jamesmontemagno/465ced24fc8a206dcaf3 to your computer and use it in GitHub Desktop.
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
}
}
}
@kolacube
Copy link

kolacube commented Feb 7, 2018

It appears that when doing a rapid motion scroll - the 'if(e.Item.ToString() == Items[Items.Count - 1])' part doesn't seem to evaluate to true... any clue as to why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment