Xamarin.forms 在Android上滚动列表视图至顶部

Xamarin.forms 在Android上滚动列表视图至顶部,xamarin.forms,Xamarin.forms,我正在使用以下命令将列表视图滚动到顶部 var appointmentGroup = appointmentGroups.First(); appointmentsList.ScrollTo(appointmentGroup.First(), appointmentGroup, ScrollToPosition.Start, true); 这将滚动以使第一组的第一项位于屏幕顶部。除此之外,我希望组的标题位于顶部 这似乎有点疯狂,但我看不出怎么做 看看来源 它似乎决定滚动到项目而不是标题。共享

我正在使用以下命令将列表视图滚动到顶部

var appointmentGroup = appointmentGroups.First();

appointmentsList.ScrollTo(appointmentGroup.First(), appointmentGroup, ScrollToPosition.Start, true);
这将滚动以使第一组的第一项位于屏幕顶部。除此之外,我希望组的标题位于顶部

这似乎有点疯狂,但我看不出怎么做

看看来源

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

共享:

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);
        }
    }
}
使用Android.Content;
使用Droid.Customization;
使用Infrastructure.UI.Xamarin;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.Android;
[程序集:ExportRenderer(typeof(ListViewScroll)、typeof(ListViewScrollRenderer))]
命名空间Droid.Customization
{
公共类ListViewScrollRenderer:ListViewRenderer
{
公共ListViewScrollRenderer(上下文):基本(上下文)
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
var list=(ListViewScroll)e.NewElement;
list.ScrollToTopImplementation=()=>
控件。平滑滚动位置(0);
}
}
}
iOS:(为了在调用代码中有一个统一的接口。)

使用Infrastructure.UI.Xamarin;
使用iOS.Customization;
使用基础;
使用UIKit;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.iOS;
[程序集:ExportRenderer(typeof(ListViewScroll)、typeof(ListViewScrollRenderer))]
命名空间iOS.Customization
{
公共类ListViewScrollRenderer:ListViewRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
var list=(ListViewScroll)e.NewElement;
list.ScrollToTopImplementation=()=>
Control.ScrollToRow(nsindepath.FromRowSection(0,0),UITableViewScrollPosition.Top,true);
}
}
}

看来有人通过使用自定义控件和简单的渲染器解决了这个问题:这正是我现在正在做的。我很抱歉。在你的问题中,我没有看到子类控件或自定义渲染器。如果您正在使用我在第一篇评论中链接到的自定义控件和渲染器,请使用该代码更新您的问题,以便我们能够更好地了解正在发生的事情。没有-我正在处理它们。我已经在回复中发布了代码。我只是想说我想到了同样的解决办法。太棒了。我误解了。当你说你现在正在做的时候,我想你的意思是你已经实现了。
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);
        }
    }
}
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);
        }
    }
}