ScrollView Both Direction android problem

This example does not work correctly in Android.

public class TestScrollViewBoth : ContentView
{

    public TestScrollViewBoth()
    {
        BackgroundColor = Color.White;
        StackLayout ss = new StackLayout();

        ScrollView sc = new ScrollView();
        sc.Orientation = ScrollOrientation.Both;

        Button b = new Button();
        b.Text = "Move";
        b.Clicked += async (sender, e2) =>
        {
            await sc.ScrollToAsync(300, 300, false);
        };

        var grid = new Grid
        {
            RowDefinitions =
            {
              new RowDefinition { Height = new GridLength(300, GridUnitType.Absolute) },
              new RowDefinition { Height = new GridLength(300, GridUnitType.Absolute) },
              new RowDefinition { Height = new GridLength(300, GridUnitType.Absolute) },
              new RowDefinition { Height = new GridLength(300, GridUnitType.Absolute) },
              new RowDefinition { Height = new GridLength(300, GridUnitType.Absolute) }
            },
            ColumnDefinitions =
            {
              new ColumnDefinition { Width = new GridLength(300, GridUnitType.Absolute) },
              new ColumnDefinition { Width = new GridLength(300, GridUnitType.Absolute) },
              new ColumnDefinition { Width = new GridLength(300, GridUnitType.Absolute) },
              new ColumnDefinition { Width = new GridLength(300, GridUnitType.Absolute) },
              new ColumnDefinition { Width = new GridLength(300, GridUnitType.Absolute) }
            }
        };

        grid.Children.Add(new Label { Text = "Hello" }, 0, 0);
        grid.Children.Add(new Label { Text = "Hello" }, 2, 1);
        grid.Children.Add(new Label { Text = "Hello" }, 2, 3);
        grid.Children.Add(new Label { Text = "Hello" }, 3, 3);
        grid.Children.Add(new Label { Text = "Hello" }, 4, 4);

        sc.Content = grid;
        ss.Children.Add(b);
        ss.Children.Add(sc);

        sc.Scrolled += (sender, e) =>
        {
            System.Diagnostics.Debug.WriteLine(e.ScrollX + " " + e.ScrollY);
        };

        this.Content = ss;

    }

}

The scrolled event returns strange values, difficult to manage.
0 44.3192464451008
337.276977183903 0
327.511719492609 0
0 12.0187786969765
313.990593458511 0
299.718293755851 0
0 0
294.460078075924 0
289.201862395997 0
282.441299378948 0

Scroll with the button moves only along the y axis.

In ios it is working properly.
Thanks for your help.

Posts

  • SunliMinSunliMin CAMember ✭✭
    edited July 2016

    Here is a bug report about it. Hopefully this gets fixed.

  • TerrenceParkTerrencePark USMember

    I was able to work around this issue by saving the ScrollX and ScrollY values when they weren't 0. Here's some code for that:

    public class TestScrollViewBoth : ContentView
    {
        double prevScrollX;
        double prevScrollY;
    
        public TestScrollViewBoth()
        {
            ...
            sc.Scrolled += (sender, e) =>
            {
                if (e.ScrollX != 0) {
                    prevScrollX = e.ScrollX;
                }
                if (e.ScrollY != 0) {
                    prevScrollY = e.ScrollY;
                }
                var scrollX = prevScrollX;
                var scrollY = prevScrollY;
    
                //do stuff with scrollX and scrollY
            };
            ...
        }
    }
    

    This code should work for iOS as well.

  • caltranenricocaltranenrico ITMember

    Scroll with the button moves only along the y axis.

        Button b = new Button();
        b.Text = "Move";
        b.Clicked += async (sender, e2) =>
        {
            await sc.ScrollToAsync(300, 300, false);
        };
    

    Thanks for reply.

Sign In or Register to comment.