在Android上滚动列表视图到顶部

问题描述 投票:0回答:1

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

var appointmentGroup = appointmentGroups.First();

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

这滚动使得第一组的第一项位于屏幕的顶部。除此之外,我希望小组的标题位于顶部。

这似乎有点疯狂,但我无论如何也看不到这样做。

看着来源code ......

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

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

xamarin.forms
1个回答
0
投票

共享:

using System;
using Xamarin.Forms;

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

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

安卓:

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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.