这看起来应该很简单,但没有 ScrollTo
TableView 上的方法与 ListView 一样。
如何将 TableView 滚动到顶部?
最佳答案
Forms 的 TableView 并非设计为像 ScrollView 一样进行控制,即使它们是使用可滚动容器 native 实现的。
虽然由于效果开销,自定义渲染器将是我显示滚动功能的首选,但快速 PlatformEffect
黑客使用 MessagingCenter
将用作演示:
在 Android 上,Forms 的 TableView 使用 Android.Widget.ListView
作为它的顶级容器,所以 ListView.SmoothScrollToPosition
可以使用。
Android PlatformEffect(使用 MessagingCenter)
public class ScrollTopEffect : PlatformEffect
{
void ScrollToTop(TableView obj)
{
(Control as AListView).SmoothScrollToPosition(0);
}
protected override void OnAttached()
{
if (Element is TableView)
MessagingCenter.Subscribe<TableView>(Element as TableView, "ScrollTop", ScrollToTop);
else
throw new Exception("ScrollTopEffect must be used on a TableView (Android.Widget.ListView)");
}
protected override void OnDetached()
{
MessagingCenter.Unsubscribe<TableView>(Element as TableView, "ScrollTop");
}
}
Xamarin.Forms 用法:
MessagingCenter.Send<TableView>(tableView, "ScrollTop");
注意:实例
tableView
var 必须是您希望滚动到顶部的那个。在 iOS 上,Forms 使用
UITableView
作为 TableView 的顶部容器,因此您可以使用 `UITableView.SetContentOffset` 滚动到顶部。iOS PlatformEffect(使用 MessagingCenter)
public class ScrollTopEffect : PlatformEffect
{
void ScrollToTop(TableView obj)
{
(Control as UITableView).SetContentOffset(CGPoint.Empty, true);
}
protected override void OnAttached()
{
if (Element is TableView)
MessagingCenter.Subscribe<TableView>(Element as TableView, "ScrollTop", ScrollToTop);
else
throw new Exception("ScrollTopEffect must be used on a TableView (UIkit.UITableView)");
}
protected override void OnDetached()
{
MessagingCenter.Unsubscribe<TableView>(Element as TableView, "ScrollTop");
}
}
关于xamarin - 如何将 TableView 滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49786486/