ListView scrolls automatically when insert elements at the top

Hello, I'm facing a problem right now using the ListView control. Whenever I insert new elements at the top, (pushing the button in the sample above) the ListView scrolls go to the first one always. I would like to know if there is a way to get a behaviour like when you Add new elements at the bottom and the scroll of the ListView doesn't move at all.

The only solution I found is scroll explicitly to the original item. (commented scrollTo line).

Here is the source code I'm using right now. (I'm not using observable collection but event driven insertion, using observable collection the behaviour is exactly the same)

    ContentPage page = new ContentPage();
        List<String> data = new List<String>()
        {
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "10",
            "11",
            "12",
            "13",
            "14",
            "15",
            "16",
            "17",
            "18",
            "19",
            "20",
            "21",
            "22",
            "23",
            "24",
            "25"
        };

        ListView list = new ListView()
        {
            ItemsSource = data

        };

        String last = "";
        list.ItemAppearing += ((sender, e) =>
        {
            last = (String)e.Item;
        });


        Button b = new Button()
        {
            Text = "button"
        };
        b.Clicked += ((sernder, e) => {
            List<String> d = new List<String>(data);
            d.Insert(0, "0");
            d.Insert(0, "-1");
            d.Insert(0, "-2");
            d.Insert(0, "-3");
            d.Insert(0, "-4");
            d.Insert(0, "-5");
            d.Insert(0, "-6");

            list.ItemsSource = d;

            //list.ScrollTo(last, ScrollToPosition.Start, false);


            data = d;
        });

        StackLayout st = new StackLayout();
        st.Children.Add(list);
        st.Children.Add(b);

        page.Content = st;


        MainPage = page;
Tagged:

Posts

  • RaphaelChiorlinRanieriRaphaelChiorlinRanieri ✭✭✭ BRMember ✭✭✭

    Hello @sisifosisifo
    Did you find a solution?

  • Sahadev_GuptaSahadev_Gupta ✭✭✭ INMember ✭✭✭

    Have you tried setting the ScrollPosition of the List..
    Here is the code. It's work fine for me
    Any List.CollectionChanged += (sender, e) => { var target = Any List[Any List.Count - 1]; (ListView x.Name).ScrollTo(target, ScrollToPosition.End, true); };

Sign In or Register to comment.