Forum Xamarin.iOS

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Listview scrolls to top automatically

ukku2215ukku2215 Member ✭✭
edited November 2020 in Xamarin.iOS

I have a groupheader listview and on tapping a itemsource expands its content.
If i tap my first item and expand it and scroll down to my next item to tap, on tapping it i am scrolled back to top.
Instead i should be able to view the item that i tapped and not scroll up all the way.
This issue is found only in ios and not in android

Tagged:

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    If i tap my first item and expand it and scroll down to my next item to tap, on tapping it i am scrolled back to top.

    How did you achieve the expandable listView? I tested a basic demo about the function, it works fine. Here is the related code, you could refer to:

    <ListView.GroupHeaderTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <StackLayout.GestureRecognizers>
                        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" CommandParameter="{Binding .}/>
                    </StackLayout.GestureRecognizers>
                    ...
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.GroupHeaderTemplate>
    

    TapGestureRecognizer_Tapped event

    private void TapGestureRecognizer_Tapped(object sender, EventArgs args)
    {
        int index = itemDataList.IndexOf(((ModelGroup)((Button)sender).CommandParameter));
        groupDataList[index].Expanded = !groupDataList[index].Expanded;
        UpdateContent();
    }
    
    private void UpdateContent()
    {
        itemDataList = new ObservableCollection<ModelGroup>();
        foreach (ItemGroup group in groupDataList)
        {
            //Create new FoodGroups so we do not alter original list
            var newGroup = new ModelGroup(...);
            ...
            if (group.Expanded)
            {
                foreach (CustomModel model in group)
                {
                    newGroup.Add(model);
                }
            }
            itemDataList.Add(newGroup);
        }
        GroupedView.ItemsSource = itemDataList;
    }
    

    You could google with the keyword as Xamarin Forms Expandable ListView - Compliance Studio to check the related tutorial.

  • ukku2215ukku2215 Member ✭✭

    @Jarvan I have implemented from the exact link - Xamarin Forms Expandable ListView - Compliance Studio you provided.

    But still i face issue in ios when i scroll my list and try to expand other item when one item is already open I am pushed all the way to the top of the list.

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited November 2020

    when i scroll my list and try to expand other item when one item is already open I am pushed all the way to the top of the list

    Check the gif I posted in the above comment, the function works fine. Please compare the function code with the sample the doc provided to get the difference. And could you share the related code to reproduce the issue?

  • ukku2215ukku2215 Member ✭✭

    I Compared the code and got it working.
    But then I am now facing the issue with listview expanding and if item is not having enough space to show up it opens abruptly in the middle of the page.

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    if item is not having enough space to show up it opens abruptly in the middle of the page.

    Hi, I tested a sample about the scrolling, it works fine. Could you post a gif to display that?

  • ukku2215ukku2215 Member ✭✭

    Hi @Jarvan
    I tried posting gif as you asked but it always said failed to upload file.
    First I clicked on first item diagnostic,
    then I scrolled down,
    then I clicked preventive, so it should have opened just like diagnostic but no, it got scrolled up all the way.

    Can we have this conversation OOO.

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    Sorry for the late reply, I'll test the scrolling as you described. You could also google with the keyword as **
    Xamarin.Forms - Expandable ListView With A Sub ListView MVVM Pattern** to try the code of the doc.

  • ukku2215ukku2215 Member ✭✭

    @Jarvan Yes I have followed "Xamarin.Forms - Expandable ListView With A Sub ListView MVVM Pattern" this project and here's what i added in it.
    I increased the number of rooms inside every sublist and made them scrollable.
    So when I open first the I scroll down to expand next sublist I am pushed to top again. This should not happen.
    I should be able to view that i tapped.

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    I cannot reproduce the issue on my side with the code. Could you share a basic sample on OneDrive?

Sign In or Register to comment.