Search code examples
androidxamarinhorizontalscrollview

How to scroll to the end immediately after adding view?


When I'm adding dynamically view to my LinearLayout which is in HorizontalScrollView and I try to scroll immediately to the end (right), it behaves like if the view wasn't yet there and scrolls to 2nd view from the end. What trick is there in xamarin android to update/refresh? In Windows Phone UpdateLayout called on scroll view did the job but I tried some combinations of Invalidate and ForceLayout and didn't succeed. When I for example put

await Task.Delay(100);

before scrolling code, it scrolls perfectly. But without it, the scrolling function doesn't see the newly added view.

This is my scrolling code:

scrollMoves.FullScroll(FocusSearchDirection.Right);

Solution

  • In java there is a method called yourView.post() which takes a runnable. Put your scrolling inside the post() method. Like :

    yourView.post(new Runnable() {
        @Override
        public void run() {
            //Here goes the scrolling code
        }
    });