Stefan Herzig
Stefan Herzig

Reputation: 41

Xamarin Forms: scroll position in scrollview

Is there no possibility to read and set the position of the scrollview? For example, I would like to start the app with the scroll position at the bottom, not at the top.

Upvotes: 4

Views: 7684

Answers (3)

MarchalPT
MarchalPT

Reputation: 1566

I've a better approach to this!

You can add this to your onAppearing() method:

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

        /*Other stuff*/

        viewModel.Messages.CollectionChanged += (sender, e) =>
        {
            var target = viewModel.Messages[viewModel.Messages.Count - 1];                

            ItemsListView.ScrollTo(target, ScrollToPosition.End, false);
        };            
    }

This means that if your collection change, you get the target which is the last item (the one in the bottom) the you use scrollTo target, position end, with animation false.

when you get to the page it will open in the bottom, you don't see the scrolling because animation is false!

Upvotes: 1

Andrew
Andrew

Reputation: 122

You can get the X and Y position of a ScrollView from the ScrollX and ScrollY properties of the ScrollView object. Then you can set the position with the ScrollView method ScrollToAsync.

var x = scrollView.ScrollX;
var y = scrollView.ScrollY;

...

bool animate = false;
scrollView.ScrollToAsync(x, y, animate);

Upvotes: 2

Stefan Herzig
Stefan Herzig

Reputation: 41

For Android, I have found a solution:

I write a custom renderer and in the OnDraw override Method, I call FullScroll(FocusSearchDirection.Down) the first time when the method is called.

What it makes easy, the Android ScrollViewRenderer is subtype of Android.Widget.ScrollView.

At the moment, I don't need an implementation for iOS, so I have no solution for iOS.

Upvotes: 0

Related Questions