简体   繁体   中英

How to detect hitting the bottom of a grid within a scrollview in Xamarin Forms

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.

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
    }
 }

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM