本文介绍了滚动到带有标题的Xamarin Forms ListView的开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xamarin Forms中滚动到ListView的顶部时遇到一些麻烦.我可以通过调用ScrollTo并传递第一项来滚动到第一项.问题是,当列表具有标题项时,我找不到滚动到标题的方法.这可能吗?我能想到的唯一解决方法是不使用标题,而在ItemSource列表的开头仅包含另一个用作标题的项目,但我更愿意在可能的情况下使用标题.谢谢.

I'm having some trouble scrolling to the top of a ListView in Xamarin Forms. I can scroll to the first item by calling ScrollTo and passing the first item. The problem is that when the list has a header item, I can't find a way to scroll to the header. Is this possible? The only work around I can think of is to not use the header and just have another item at the start of the ItemSource list that acts as a header but I'd rather use the header if possible. Thanks.

推荐答案

所以我现在已经解决了这个问题.我的解决方案是继承ListView的子类,并添加一个公共ScrollToTop方法,该方法在调用时将调用内部ScrollToTopRequestedEvent.然后,我在每个平台上将ListViewRenderer子类化,并为该事件注册.

So I've solved this myself now. My solution was to subclass ListView and add a public ScrollToTop method which invokes an internal ScrollToTopRequestedEvent when called. I then subclassed the ListViewRenderer on each platform and registered for the event.

然后在Android渲染器中调用Control.SmoothScrollToPositionFromTop(0,0)滚动到顶部.

In the Android renderer I'm then calling Control.SmoothScrollToPositionFromTop(0, 0) to scroll to top.

在渲染的iOS中,我调用Control.ScrollRectToVisible(new CoreGraphics.CGRect(0,0,1,1),true).

In the iOS rendered I'm calling Control.ScrollRectToVisible(new CoreGraphics.CGRect(0, 0, 1, 1), true).

这篇关于滚动到带有标题的Xamarin Forms ListView的开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:05