Reputation: 548
I am using Xamarin.Forms to display Items from a Websource in a Grid (within a ScrollView).
When the user hits the bottom of the grid, i want to load more items and add them to the grid. I know that usually a ListView is preferred for displaying data in this fashion (and that ListView has an ItemAppearing event) but sadly i have to use a grid.
If it helps, i could post some code, but im not sure if that is necessary here.
Thanks in advance Edit: here is my really boring layout file:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyProject.MyNamespace.LayoutName"
SizeChanged="OnPageSizeChanged">
<ScrollView >
<AbsoluteLayout x:Name="myLayout">
<Grid x:Name="GridForItems" RowSpacing="6" ColumnSpacing="6">
</Grid>
</AbsoluteLayout>
</ScrollView>
</ContentPage>
I add all the rows, columns and items programmatically.
Upvotes: 1
Views: 2481
Reputation: 548
Since i had not gotten a satisfying answer to my original question (how to detect hitting the bottom of a scrollview) and i have not found a solution anywhere else on the net i am going to post how i solved this.
Note that this is probably the not an elegant solution and, if you have a better answer, please post it and i will accept it as the best answer.
What i did was implement a delegate for the Scrolled Event of ScrollView.
myScrollView.Scrolled += (sender, e) => { onScrolled(); };
Within my onScrolled i have the following if-clause to determine whether i have hit the bottom of the ScrollView:
private void onScrolled()
{
if(myScrollView.ScrollY >= myScrollView.ContentSize.Height - myScrollView.Height)
{
//Handle hitting the bottom
}
}
Upvotes: 4
Reputation: 3701
When you have a list of items you should use a ListView
to display it. In the DataTemplate
of this ListView you can specify how to display each item (use a grid there if you really need a grid).
In the Listview, you can implement following code to detect the bottom:
myListview.ItemAppearing += (sender, e) =>
{
// CHECK HERE
if(e.Item.Id == MyItems[MyItems.Count - 1].Id)
{
// YOU HIT THE BOTTOM
}
}
Why should I use Listview? Look at this forum thread about Dynamic Grids.
Upvotes: 0