【发布时间】:2018-09-24 18:51:45
【问题描述】:
我在 Xamarin Forms 中滚动到 ListView 顶部时遇到了一些问题。我可以通过调用 ScrollTo 并传递第一项来滚动到第一项。问题是当列表有标题项时,我找不到滚动到标题的方法。这可能吗?我能想到的唯一解决方法是不使用标题,而只是在 ItemSource 列表的开头有另一个项目作为标题,但如果可能的话,我宁愿使用标题。谢谢。
【问题讨论】:
标签: xamarin xamarin.forms
我在 Xamarin Forms 中滚动到 ListView 顶部时遇到了一些问题。我可以通过调用 ScrollTo 并传递第一项来滚动到第一项。问题是当列表有标题项时,我找不到滚动到标题的方法。这可能吗?我能想到的唯一解决方法是不使用标题,而只是在 ItemSource 列表的开头有另一个项目作为标题,但如果可能的话,我宁愿使用标题。谢谢。
【问题讨论】:
标签: xamarin xamarin.forms
所以我现在自己解决了这个问题。我的解决方案是继承 ListView 并添加一个公共 ScrollToTop 方法,该方法在调用时调用内部 ScrollToTopRequestedEvent。然后,我将每个平台上的 ListViewRenderer 子类化并注册了该事件。
然后我在 Android 渲染器中调用 Control.SmoothScrollToPositionFromTop(0, 0) 滚动到顶部。
在渲染的 iOS 中,我调用 Control.ScrollRectToVisible(new CoreGraphics.CGRect(0, 0, 1, 1), true)。
【讨论】:
哇,感谢@Gareth Wynn,真是太酷了。 无论如何,这是每个人都可以使用的代码,更改类名和命名空间,不包括 iOS,与 Android 一样,只是使用 Gareth Wynn 的提示并行回答:
共享NiftyListView.cs:
using System;
using Xamarin.Forms;
namespace AppoMobi
{
public class NiftyListView : CListView
{
public event EventHandler EventScrollToTop;
//-------------------------------------------------------------------
public void ScrollToTop(bool animate=true)
//-------------------------------------------------------------------
{
//bool animate is not used at this stage, it's always animated.
EventScrollToTop?.Invoke(this, EventArgs.Empty);
}
}
}
安卓NiftyListView.Android.cs:
using System;
using AppoMobi;
using AppoMobi.Droid.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using ListView = Xamarin.Forms.ListView;
[assembly: ExportRenderer(typeof(NiftyListView), typeof(NiftyListViewRenderer))]
namespace AppoMobi.Droid.Renderers
{
//-------------------------------------------------------------------
class NiftyListViewRenderer : ListViewRenderer
//-------------------------------------------------------------------
{
protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
{
base.OnElementChanged(e);
var view = (NiftyListView)Element;
if (view == null) return;
view.EventScrollToTop += OnScrollToTop;
}
//-------------------------------------------------------------------------------------------
public async void OnScrollToTop(object sender, EventArgs eventArgs)
//-------------------------------------------------------------------------------------------
{
Control.SmoothScrollToPositionFromTop(0, 0);
}
}
}
【讨论】:
ScrollTo(Object, Object, ScrollToPosition, Boolean) 将 ListView 滚动到组中的项目 https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.listview.scrollto?view=xamarin-forms
每个组的模型:
public class Notification {
public int Id { get; set; }
public string Title { get; set; };
public Notification(int id, string title) {
Id = id;
Title = title;
}
}
ItemSource 的组模型:
public class NotificationGroup: List<Notification> {
public string Title { get; set; }
public string ShortTitle { get; set; }
public NotificationGroup(string title) {
Title = title;
}
}
示例数据和使用情况:
//SAMPLE DATA
var notifications = new ObservableCollection <NotificationGroup> {
new NotificationGroup("Group-01") {
new Notification(1, "Item-1"),
new Notification(2, "Item-2")
},
new NotificationGroup("Group-02") {
new Notification(3, "Item-3"),
new Notification(4, "Item-4")
}
};
YourListViewName.ItemSource = notifications;
//USING
YourListViewName.ScrollTo(notifications.First()[0], notifications.First(), ScrollToPosition.Start, true);
【讨论】: