question

SimonGhost-8216 avatar image
0 Votes"
SimonGhost-8216 asked JarvanZhang-MSFT commented

How to Check Listview Hit bottom with ObservableCollection Xamarin.Forms

Hello Everyone, I searched a lot about how to make List view with (Load More) Feature but i have problem with check list view hit bottom or the last item of listview because i want to show 10 items in listview and check if listview hit the last item and load another 10 items...
so this my xaml.cs file:

 ObservableCollection<WorldData> ADITM = new ObservableCollection<WorldData>();
 public ObservableCollection<WorldData> ls1 { get { return ADITM; } }
 public class WorldData
 {
     public string Name { set; get; }
     public string Image { set; get; }
 }

and i have problem in Listview ItemAppearing event:

  private void MyListview_ItemAppearing(object sender, ItemVisibilityEventArgs e)
         {
             if (isLoading || ADITM.Count == 0)
                 return;
    
             //when Listview hit bottom
             if (e.Item.ToString() == ADITM[ADITM.Count - 1])
             {
                 LoadMore();
             }
         }

output Error is:
Operator '==' cannot be applied to operands of type 'string' and 'MainWorlds.WorldData'

how to fix this Error and thanks :) and what the best way to show large data in listview ? is load all json data and show 10 items and when hit bottom show another 10 ? or limit json to get 10 items and request another 10 items and show it










dotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

Hello SimonGhost-8216,​

Welcome to our Microsoft Q&A platform!

output Error is: Operator '==' cannot be applied to operands of type 'string' and 'MainWorlds.WorldData'

This is because e.Item.ToString() returns a string value, but e.Item is type of the 'WorldData' in your project. In the ItemAppearing event, e.Item is the type of Model data. You could use it directly to detect it the bottom item is the last item. Or you can also use e.ItemIndex to check the index of the bottom item.

Here is the sample code, you could refer to it.

private void ListView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
{
    if (e.Item == ADITM[ADITM.Count - 1])
    {
        LoadMore();
    }

    //or use e.ItemIndex to detect if it's the last item
    //if (e.itemindex == aditm.count - 1)
    //{
    //    loadmore();
    //}
}


Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much :) You made this easy I appreciate you

0 Votes 0 ·

You're welcome.

0 Votes 0 ·