将列表视图滚动到Android顶部

时间:2018-09-11 15:28:24

标签: xamarin.forms

我正在使用以下内容将列表视图滚动到顶部...

var appointmentGroup = appointmentGroups.First();

appointmentsList.ScrollTo(appointmentGroup.First(), appointmentGroup, ScrollToPosition.Start, true);

滚动以使第一组的第一项在屏幕顶部。除此以外,我希望该组的标题位于顶部。

这似乎有些疯狂,但我还是看不到这样做。

查看来源code ...

position = templatedItems.GetGlobalIndexForGroup(group) + results.Item2 + 1;

似乎确定要滚动到项目而不是标题。

1 个答案:

答案 0 :(得分:0)

共享:

using System;
using Xamarin.Forms;

namespace Infrastructure.UI.Xamarin
{
    public class ListViewScroll : ListView
    {
        public Action ScrollToTopImplementation;

        public void ScrollToTop() => ScrollToTopImplementation();
    }
}

Android:

using Android.Content;
using Droid.Customization;
using Infrastructure.UI.Xamarin;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(ListViewScroll), typeof(ListViewScrollRenderer))]

namespace Droid.Customization
{
    public class ListViewScrollRenderer : ListViewRenderer
    {
        public ListViewScrollRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
        {
            base.OnElementChanged(e);

            var list = (ListViewScroll) e.NewElement;

            list.ScrollToTopImplementation = () =>
                Control.SmoothScrollToPosition(0);
        }
    }
}

iOS :(为了在调用代码中具有统一的界面。)

using Infrastructure.UI.Xamarin;
using iOS.Customization;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(ListViewScroll), typeof(ListViewScrollRenderer))]

namespace iOS.Customization
{
    public class ListViewScrollRenderer : ListViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
        {
            base.OnElementChanged(e);

            var list = (ListViewScroll) e.NewElement;

            list.ScrollToTopImplementation = () =>
                Control.ScrollToRow(NSIndexPath.FromRowSection(0, 0), UITableViewScrollPosition.Top, true);
        }
    }
}