【问题标题】:Can't scroll ListView to top on iOS?无法在 iOS 上将 ListView 滚动到顶部?
【发布时间】:2018-11-22 23:57:24
【问题描述】:

在 Xamarin Forms 3.3 上,我无法在 iOS 上将 ListView 滚动到顶部。

我试试:Scrolling to start of Xamarin Forms ListView with header 但不能将滚动 ListView 设置为顶部。 这是我的代码:

在控制 iOS:

public class ExtListViewRenderer : ListViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<ListView> e) => iOS.Control.Logger.InvokeAction(() =>
    {
        base.OnElementChanged(e);

        if (!(e.NewElement is ExtListView extListView) || Control == null)
            return;

        extListView.EventScrollToTop += OnScrollToTop;
    });

    public void OnScrollToTop(object sender, EventArgs eventArgs)
    {
        Control.ScrollRectToVisible(new CoreGraphics.CGRect(0, 0, 1, 1), true);
    }

}

课堂控制:

public class ExtListView : ListView
{
    public event EventHandler EventScrollToTop;

    /// <summary>
    /// Scroll LisView To Top
    /// </summary>
    /// <param name="animate"></param>
    public void ScrollToTop(bool animate = true)
    {
        //bool animate is not used at this stage, it's always animated.
        EventScrollToTop?.Invoke(this, EventArgs.Empty);
    }
}

在视图 .xaml:

    <control:ExtListView.Behaviors>
        <common:MethodToDelegateBehavior Delegate="{Binding ScrollToTop}"
                                         DelegateType="{x:Type propertyViewModels:PropertyPageViewModel+ScrollToTopDelegate}"
                                         MethodName="ScrollToTop" />
    </control:ExtListView.Behaviors>

在页面:

    public delegate void ScrollToTopDelegate(bool animate);
    private ScrollToTopDelegate _scrollToTop;
    public ScrollToTopDelegate ScrollToTop
    {
        get => _scrollToTop;
        set => this.RaiseAndSetIfChanged(ref _scrollToTop, value);
    }

方法使用时:

ScrollToTop?.Invoke(true);
  • 我在 Android 上使用它可以工作,但 iOS 不工作。

【问题讨论】:

  • 我在没有使用 ScrollToTopDelegate 的情况下测试了您的代码,它运行良好。当我调用ScrollToTop 函数时,listView 滚动到顶部。您能否提供更多信息以重现您的问题?

标签: listview xamarin xamarin.forms xamarin.ios


【解决方案1】:

我使用以下代码将我的 ListView 类滚动到特定位置。

Control.SetContentOffset(new CGPoint(Control.ContentOffset.X, y), animated);

滚动时,X 通常不会改变,但 Y 是您想要改变的值。使用该代码滚动到顶部将是这样的:

// You can send true for the animate property whether or not you want to see the scroll happen
Control.SetContentOffset(new CGPoint(Control.ContentOffset.X, 0), true);

【讨论】:

  • iOS 有时可以在 ListView 上设置为顶部。但它的第一次显示不能设置为顶部。 @TaylorD
【解决方案2】:

这是我的想法:

  • 使用ListView类(ScrollTo)的方法ScrollTo
  • 将项目焦点设置在ListView 是第一项,ScrollToPositionScrollToPosition.Start

(如果你想看到滚动发生,请使用animated = true

创建一个标志:ScrollToTop = true 在布局 .xaml 中使用该处理程序更改 ListView 的滚动。

你试试看:

 var firstItem = ((IEnumerable<object>)extListView.ItemsSource)?.FirstOrDefault();
 if (firstItem != null)
     extListView.ScrollTo(firstItem, ScrollToPosition.Start, false);

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多