Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

CollectionView: detect there's no scroll / has scroll / scroll bottom

SergTomcatSergTomcat Member ✭✭

I have to find current "scroll" state whether there's no scroll, or scroll active and in a certain position, affecting visibility and behavious of other controls. There three cases:

  1. All loaded items are fitted inside CollectionView area and there is no scroll needed.

  1. Items took more space than available and vertical scroll appeared

  1. Scroll hit the bottom

Last two can be achieved via RemainingItemsThreshold property and events, but they don't work in UWP.

And I cannot figure out how to detect if there's any scroll at all.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Yes, this RemainingItemsThreshold not work in UWP. This issue is reported in the github.

    https://github.com/xamarin/Xamarin.Forms/issues/9013

    Here is a workaround. Here is running GIF.

    You can use Scrolled event to achieve it

     <CollectionView x:Name="MyCollection" HeightRequest="150"  Scrolled="MyCollection_Scrolled"  BackgroundColor="Green">
    
            </CollectionView>
    

    Background code.

      private void MyCollection_Scrolled(object sender, ItemsViewScrolledEventArgs e)
            {
                if (Device.RuntimePlatform != Device.UWP)
                {
                    return;
                }
    
                if (sender is CollectionView cv)
                {
                    int lastindex = e.LastVisibleItemIndex+1;
                    mylabel1.Text = "Current LastVisibleItem  " + lastindex.ToString();
                    if (str.Length > lastindex)
                    {
                        // have scrollbar
    
                    }
                    if ((lastindex)== str.Length)
                    {
    
                        mylabel2.Text = "Scroll to the end";
                    }
    
                }
            }
        }
    
  • SergTomcatSergTomcat Member ✭✭

    @LeonLu, thank you for the workaround.

    However, Scrolled event doesn't fire if elements all fit initially and no scroll present. And if I start add new items to ObservableCollection, Scrolled event also doesn't fire, although new items already reached bottom and vertical scroll appeared. Sadly, Scrolled triggers only with user interaction or ScrollTo method.

    P.S.
    str in your example is ItemSource?

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited July 21

    str in your example is ItemSource?

    Yes it is .

    str = new string[]
    {
        "Baboon",
        "Capuchin Monkey",
        "Blue Monkey",
        "Squirrel Monkey",
        "Golden Lion Tamarin",
        "Howler Monkey",
        "Japanese Macaque"
    };
                mylabel.Text = "counts in string[] " + str.Length.ToString();
                MyCollection.ItemsSource = str;
    
Sign In or Register to comment.