xamarin.androidandroid-scrollview

Scroll to bottom of layout


I am trying to scroll to bottom of layout. I've got this piece of code, but it's not working

         ScrollView scrollView = view.FindViewById<ScrollView>(Resource.Id.scrollViewHelper);
         scrollView.FullScroll(FocusSearchDirection.Down);

Solution

  • A simple way is to create an Action-based runnable, passing the Action as a parameter to the runnable . You can refer to the following code:

            scrollView = FindViewById<ScrollView>(Resource.Id.scrollView1);
    
            var runnable = new MyRunnable(async () =>
            {
                // Do whatever you need to do, including capturing of local vars, app/activity context, etc.
                await Task.Delay(500);
                scrollView.FullScroll(FocusSearchDirection.Down);
            });
            runnable.Run();
    

    Code of class MyRunnable

    public class MyRunnable : Java.Lang.Object, Java.Lang.IRunnable
    {
        readonly WeakReference<Action> actionRef;
    
        public MyRunnable(Action action)
        {
            actionRef = new WeakReference<Action>(action);
        }
    
        public void Run()
        {
            actionRef.TryGetTarget(out Action action);
            action?.Invoke();
        }
    }