【问题标题】:Xamarin Forms - detect when user starts to scroll down in a scrollviewXamarin Forms - 检测用户何时开始在滚动视图中向下滚动
【发布时间】:2019-02-19 11:18:53
【问题描述】:

我有一个滚动视图,一旦用户开始向上滚动,我就会运行一些代码 (OnSwipeUp())。我使用

检测到这一点
textScroll.Scrolled += (sender, e) => { onScrolled(); };

private void onScrolled()
{
      if(textScroll.ScrollY > 0)
      {
          OnSwipeUp(null, null);
      }
}

我还需要检测用户何时开始向下滚动,所以我想获取滚动视图的最大 Y 位置然后说类似

if(textScroll.ScrollY < maxY)

我能做到吗?

【问题讨论】:

    标签: c# xamarin.forms scrollview


    【解决方案1】:

    试试这个:

    private double previousScrollPosition = 0;
    void Handle_Scrolled(object sender, Xamarin.Forms.ScrolledEventArgs e) 
    {
    
      if (previousScrollPosition < e.ScrollY) 
      {
        //scrolled down
        previousScrollPosition = e.ScrollY;
      } 
      else 
      {
          //scrolled up
    
        if (Convert.ToInt16(e.ScrollY) == 0)
        previousScrollPosition = 0;
    
      }
    }
    

    【讨论】:

    • 感谢您的发帖,但我认为这将在滚动完成后开始?如果可能的话,我需要在滚动开始时进行锻炼
    • 不,只需将断点放在 if 和 else 块中,一旦滚动开始就会命中。
    【解决方案2】:

    您不需要获取最大 Y 位置,因为在向下滚动之前,Y 位置始终为 0。因此您只需要获取滚动方向即可。 我写了一个演示来证明这一点:

    MainPage.xaml.cs

    private double previousScrollPosition = 0;
    
            public MainPage()
            {
                InitializeComponent();
            }
    
            void Handle_Scrolled(object sender, Xamarin.Forms.ScrolledEventArgs e)
            {
                if (previousScrollPosition < e.ScrollY)
                {
                    //scrolled down
                    Console.WriteLine("!!!!!!!!!scrolled down   ScrollY=>" + textScroll.ScrollY);
                }
                else
                {
                    //scrolled up
                    Console.WriteLine("!!!!!!!!!scrolled up   ScrollY=>" + textScroll.ScrollY);
                }
                previousScrollPosition = e.ScrollY;
            }
    

    MainPage.xaml

    <ContentPage.Content>
            <StackLayout>
                <BoxView BackgroundColor="Red" HeightRequest="200" WidthRequest="150" />
                <ScrollView x:Name="textScroll" Scrolled="Handle_Scrolled">
                    <StackLayout>
                        <Entry Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
                        <Entry Text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"/>
                        <Entry Text="ccccccccccccccccccccccccccccccccccccccccccccccccccc"/>
                        <Entry Text="ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"/>
                        <Entry Text="eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"/>
                        <Entry Text="fffffffffffffffffffffffffffffffffffffffffff"/>
                        <Entry Text="ggggggggggggggggggggggggggggggggggggggggggggg"/>
                        <Entry Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
                        <Entry Text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"/>
                        <Entry Text="ccccccccccccccccccccccccccccccccccccccccccccc"/>
                        <Entry Text="dddddddddddddddddddddddd"/>
                        <Entry Text="eeeeeeeeeeeeeeeeeeeeeeeeee"/>
                        <Entry Text="fffffffffffffffffffffffffffffffffffffffffff"/>
                        <Entry Text="ggggggggggggggggggggggggggggggggggggggggggggg"/>
                        <Entry Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
                        <Entry Text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"/>
                        <Entry Text="ccccccccccccccccccccccccccccccccccccccccccccccccccc"/>
                        <Entry Text="ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"/>
                        <Entry Text="eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"/>
                        <Entry Text="fffffffffffffffffffffffffffffffffffffffffff"/>
                        <Entry Text="ggggggggggggggggggggggggggggggggggggggggggggg"/>
                        <Entry Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
                        <Entry Text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"/>
                        <Entry Text="ccccccccccccccccccccccccccccccccccccccccccccc"/>
                        <Entry Text="dddddddddddddddddddddddd"/>
                        <Entry Text="eeeeeeeeeeeeeeeeeeeeeeeeee"/>
                        <Entry Text="fffffffffffffffffffffffffffffffffffffffffff"/>
                        <Entry Text="ggggggggggggggggggggggggggggggggggggggggggggg"/>
                    </StackLayout>
                </ScrollView>
    
            </StackLayout>
    
        </ContentPage.Content>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-29
      相关资源
      最近更新 更多