【问题标题】:How to detect list view scrolling direction in xamarin forms如何检测 xamarin 表单中的列表视图滚动方向
【发布时间】:2017-05-17 05:56:03
【问题描述】:

我尝试检测列表视图的滚动方向。我的要求是需要在列表视图向上和向下滚动时实现不同的功能。请提出任何检测列表视图滚动方向的想法。我在列表视图中尝试了以下语法。

示例代码:

 <StackLayout>
    <Label x:Name="Direction" />
    <ListView ItemsSource="{Binding Items}" HasUnevenRows = "true" ItemAppearing="Handle_ItemAppearing" IsPullToRefreshEnabled = "true">
    <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                    <Label Text = "{Binding}" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
    </ListView.ItemTemplate>
        </ListView>
</StackLayout>

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    我认为默认情况下您无法做到,您只能对正在出现或消失的项目进行操作。因此,您要么需要通过创建一些代码来获取(不)出现项目的索引,并查看索引是变高还是变低,以确定某人是向上还是向下滚动。或者你需要连接一个自定义渲染器,但我不确定原生控件是否也能检测到这一点。

    我为你准备了一个非常基本的例子,你可以找到full code here

    基本上挂钩到可用事件,跟踪类变量中的最后一个索引,并将其与正在出现的项目的当前索引进行比较。

    private void Handle_ItemAppearing (object sender, Xamarin.Forms.ItemVisibilityEventArgs e)
    {
        var currentIdx = Items.IndexOf ((string)e.Item);
    
        if (currentIdx > _lastItemAppearedIdx)
            Direction.Text = "Up";
        else
            Direction.Text = "Down";
    
        _lastItemAppearedIdx = Items.IndexOf ((string)e.Item);
    }
    

    在这段代码中,我只是在Label 中显示它,但当然您可以创建一些枚举来返回或触发事件或其他东西,以使代码更可重用。下面是实际代码:

    【讨论】:

    • 您好 Gerald Versluis,感谢您的回复。您建议了 ItemAppearing 事件,但我已经尝试过,它仅在列表中加载数据时命中,然后当我们尝试滚动内容时,它不再命中。
    • 很抱歉,您做错了,每次出现项目时都会触发此事件,运行随附的 GitHub 示例,它可以工作。
    • 我的要求是在列表视图中加载数据后,当用户尝试滚动数据时需要检测他是向上还是向下滚动。
    • 抱歉 Gerald,我已经运行了该示例代码,它运行良好,但我不明白为什么列表视图不会触发 itemappearing 事件。
    • 现在可以用了吗?我不太明白你最后的评论。
    【解决方案2】:

    最近遇到了这个问题并以这种方式解决了它:

    <StackLayout>
    <Label x:Name="Direction" />
    <ListView ItemsSource="{Binding Items}" HasUnevenRows = "true" ItemAppearing="Handle_ItemAppearing" ItemDisappearing="Handle_ItemDisappearing" IsPullToRefreshEnabled = "true">
    <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                    <Label Text = "{Binding}" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
    </ListView.ItemTemplate>
        </ListView>
    

        string ScrollingDirection;
        int visibleTabIndex;
        int disappearingTabIndex;
    
        public async void Handle_ItemAppearing(object sender, ItemVisibilityEventArgs e)
        {
            var visibleTab = e.Item;
            visibleTabIndex = MyItemsList.IndexOf(visibleTab);
            if (disappearingTabIndex > visibleTabIndex) ScrollingDirection = "DOWN";
            else ScrollingDirection = "UP";
    
        }
    
        public async void Handle_ItemDisappearing(object sender, ItemVisibilityEventArgs e)
        {
            var invisibleTab = e.Item as TicketsList;
            disappearingTabIndex = tvm.Tickets.IndexOf(invisibleTab);
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      相关资源
      最近更新 更多