Home > database >  Xamarin: Dynamically scroll to bottom after item added in ObservableCollection without using ItemsUp
Xamarin: Dynamically scroll to bottom after item added in ObservableCollection without using ItemsUp

Time:11-25

This code doesn't seem to work

function ScrollToBottom(int listCount){
     this.FindByName<CollectionView> 
        ("MyCollectioNView").ScrollTo(listCount- 1, animate: false);
}

Note: The function will be called inside MessagingCenter.Subscribe() What could be the workaround for this problem?

CodePudding user response:

I did try to reproduce the problem in this way, but I didn't reproduce the problem.

I did a test based on the official code VerticalListSinglePreSelectionPage.xaml.cs and modified code as follows:

public partial class VerticalListSinglePreSelectionPage : ContentPage 
{
    MonkeysViewModel viewModel;
    public VerticalListSinglePreSelectionPage()
    {
        InitializeComponent();

        viewModel = new MonkeysViewModel();
        BindingContext = viewModel;
    }

    private void Button_Clicked(object sender, System.EventArgs e)
    {

        viewModel.Monkeys.Add(new Monkey
        {
            Name = "Howler Monkey",
            Location = "South America",
            Details = "Howler monkeys are among the largest of the New World monkeys. Fifteen species are currently recognised. Previously classified in the family Cebidae, they are now placed in the family Atelidae.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Alouatta_guariba.jpg/200px-Alouatta_guariba.jpg"
        });

        viewModel.Monkeys.Add(new Monkey
        {
            Name = "Japanese Macaque",
            Location = "Japan",
            Details = "The Japanese macaque, is a terrestrial Old World monkey species native to Japan. They are also sometimes known as the snow monkey because they live in areas where snow covers the ground for months each",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Macaca_fuscata_fuscata1.jpg/220px-Macaca_fuscata_fuscata1.jpg"
        });

        viewModel.Monkeys.Add(new Monkey
        {
            Name = "Mandrill",
            Location = "Southern Cameroon, Gabon, Equatorial Guinea, and Congo",
            Details = "The mandrill is a primate of the Old World monkey family, closely related to the baboons and even more closely to the drill. It is found in southern Cameroon, Gabon, Equatorial Guinea, and Congo.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/7/75/Mandrill_at_san_francisco_zoo.jpg/220px-Mandrill_at_san_francisco_zoo.jpg"
        });

        viewModel.Monkeys.Add(new Monkey
        {
            Name = "Proboscis Monkey",
            Location = "Borneo",
            Details = "The proboscis monkey or long-nosed monkey, known as the bekantan in Malay, is a reddish-brown arboreal Old World monkey that is endemic to the south-east Asian island of Borneo.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Proboscis_Monkey_in_Borneo.jpg/250px-Proboscis_Monkey_in_Borneo.jpg"
        });

        viewModel.Monkeys.Add(new Monkey
        {
            Name = "Red-shanked Douc",
            Location = "Vietnam, Laos",
            Details = "The red-shanked douc is a species of Old World monkey, among the most colourful of all primates. This monkey is sometimes called the \"costumed ape\" for its extravagant appearance. From its knees to its ankles it sports maroon-red \"stockings\", and it appears to wear white forearm length gloves. Its attire is finished with black hands and feet. The golden face is framed by a white ruff, which is considerably fluffier in males. The eyelids are a soft powder blue. The tail is white with a triangle of white hair at the base. Males of all ages have a white spot on both sides of the corners of the rump patch, and red and white genitals.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/Portrait_of_a_Douc.jpg/159px-Portrait_of_a_Douc.jpg"
        });

        viewModel.Monkeys.Add(new Monkey
        {
            Name = "Gray-shanked Douc",
            Location = "Vietnam",
            Details = "The gray-shanked douc langur is a douc species native to the Vietnamese provinces of Quảng Nam, Quảng Ngãi, Bình Định, Kon Tum, and Gia Lai. The total population is estimated at 550 to 700 individuals. In 2016, Dr Benjamin Rawson, Country Director of Fauna & Flora International - Vietnam Programme, announced a discovery of an additional population of more than 500 individuals found in Central Vietnam, bringing the total population up to approximately 1000 individuals.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Cuc.Phuong.Primate.Rehab.center.jpg/320px-Cuc.Phuong.Primate.Rehab.center.jpg"
        });

        viewModel.Monkeys.Add(new Monkey
        {
            Name = "Golden Snub-nosed Monkey",
            Location = "China",
            Details = "The golden snub-nosed monkey is an Old World monkey in the Colobinae subfamily. It is endemic to a small area in temperate, mountainous forests of central and Southwest China. They inhabit these mountainous forests of Southwestern China at elevations of 1,500-3,400 m above sea level. The Chinese name is Sichuan golden hair monkey. It is also widely referred to as the Sichuan snub-nosed monkey. Of the three species of snub-nosed monkeys in China, the golden snub-nosed monkey is the most widely distributed throughout China.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Golden_Snub-nosed_Monkeys,_Qinling_Mountains_-_China.jpg/165px-Golden_Snub-nosed_Monkeys,_Qinling_Mountains_-_China.jpg"
        });

        viewModel.Monkeys.Add(new Monkey
        {
            Name = "Black Snub-nosed Monkey",
            Location = "China",
            Details = "The black snub-nosed monkey, also known as the Yunnan snub-nosed monkey, is an endangered species of primate in the family Cercopithecidae. It is endemic to China, where it is known to the locals as the Yunnan golden hair monkey and the black golden hair monkey. It is threatened by habitat loss. It was named after Bishop Félix Biet.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/5/59/RhinopitecusBieti.jpg/320px-RhinopitecusBieti.jpg"
        });

        // MyCollectioNView.ScrollTo(viewModel.Monkeys.Count-1,animate:false);


        ScrollToBottom(viewModel.Monkeys.Count);

    }

    void ScrollToBottom(int listCount)
    {
        this.FindByName<CollectionView>
           ("MyCollectioNView").ScrollTo(listCount - 1, animate: false);
    }
}

CodePudding user response:

Workaround for this issue

this.FindByName<CollectionView>("MyCollectionView").ScrollTo(listCount   1, animate: true);

Seems do the trick.

  • Related