How to Load More Items at End of ListView in Xamarin.Forms

Inwizards-blog-img

Ofen we face problem at the time when we code in xamarin for lode more than 10 items in list. The page takes too much time to load, if we have many items to see in the listview. Suppose we have 500 items or more in our listview to show, then it will take so much time to lode page until all 500 items will be loaded. Today I am going to show you how to load more items at End of listView in Xamarin.Forms

Lets Starts:

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

}

}

}

How to Load More Items at End of ListView in Xamarin.Forms

Post navigation


0 0 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback
Homepage
6 years ago

… [Trackback]

[…] Read More Infos here: inwizards.com/blog/how-to-load-items-at-end-listview-xamarin-forms/ […]

1
0
Would love your thoughts, please comment.x
()
x