【问题标题】:Is there a way to detect whether CollectionView scrolling has been initiated by the user or ScrollTo?有没有办法检测 CollectionView 滚动是由用户还是 ScrollTo 发起的?
【发布时间】:2020-02-27 13:06:01
【问题描述】:

我有一个托管CollectionViewMap 的页面。 有一个项目列表,CollectionViewItemsSource 设置为该列表并基于此列表绘制地图图钉,要求当用户滚动CollectionView 时,突出显示对面的地图图钉,当点击 pin 时,CollectionView 会滚动到该项目。

我使用ScrollToScrolled 事件。但问题是当ScrollTo 被调用时,Scrolled 事件也会被触发,这会导致延迟或一些意外行为。 我试图设置一个标志:

private int centerIndex = -1;
bool userScroll;

private void CollectionView_Scrolled(object sender, ItemsViewScrolledEventArgs args)
{
    if (centerIndex != args.CenterItemIndex)
    {
        if (userScroll)
            MessagingCenter.Send<object, int>(this, Keys.GoToLocation, args.CenterItemIndex);
        userScroll = true;
        centerIndex = args.CenterItemIndex;
    }
}


private void ScrollToVehicle(object arg1, Item item)
{

    if (collectionView.ItemsSource != null && collectionView.ItemsSource.Cast<Item>().Contains(item))
    {
        userScroll = false;
        collectionView.ScrollTo(item, position: ScrollToPosition.Center, animate: false);
    }
}

但问题是Scrolled 事件被多次调用(在if 语句内)

【问题讨论】:

  • 我猜你想在滚动到特殊位置时突出显示,你可以使用ScrollTo方法而不是Scrolled
  • @WendyZang-MSFT 我需要同时使用两者。我不想在请求 ScrollTo 时触发 Scrolled 事件

标签: xamarin.forms collectionview


【解决方案1】:

在调用 ScrollTo 之前尝试取消订阅 CollectionView_Scrolled

collection.Scrolled -= CollectionView_Scrolled;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 2021-05-12
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多