0
votes

In Xamarin Forms i have a ListView and the following method in Code Behind:

    protected async override void OnAppearing()
    {
        base.OnAppearing();

        events.ItemsSource = await App.ServiceManager.GetStream();
    }

where

events

is my ListView and fetch data from rest webservice.

When i select an item in the ListView i push a detail page. The problem is that when i pop back to the ListView, the method OnAppearing() is called and make the remote call again.

Instead i'd like that the scroll start from the previous position (before that i push a new page): how can do that?

2
Move the logic to ViewModel and create a single instance of it - for example.EvZ

2 Answers

1
votes

I think you can simply use a

bool isFirstAppearing = true;

protected async override void OnAppearing()
{
    base.OnAppearing();

    if(isFirstAppearing) {
         isFirstAppearing = false;
         events.ItemsSource = await App.ServiceManager.GetStream();
    }
}
0
votes

You can either do a check if its already been loaded, as alessandro Caliaros answer suggests, or an alternative if you want to get fresh data but still be scrolled in the same position, you can add code in onappearing to set the scroll position to be the same as it was before:

var currentYPosition = _scrollView.ScrollY;
await _scrollView.ScrollToAsync(_scrollView.ScrollX, currentYPosition, true);