滚动到带有标题的 Xamarin Forms ListView 的开头 [英] Scrolling to start of Xamarin Forms ListView with header

查看:21
本文介绍了滚动到带有标题的 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 的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆