Gar*_*ynn 7 xamarin xamarin.forms
我在Xamarin Forms中滚动到ListView的顶部时遇到了一些麻烦.我可以通过调用ScrollTo并传递第一个项目来滚动到第一个项目.问题是,当列表有一个标题项时,我找不到滚动到标题的方法.这可能吗?我能想到的唯一工作就是不使用标题,只在ItemSource列表的开头有另一个项目作为标题,但我宁可使用标题.谢谢.
Gar*_*ynn 14
所以我现在自己解决了这个问题.我的解决方案是继承ListView并添加一个公共ScrollToTop方法,该方法在调用时调用内部ScrollToTopRequestedEvent.然后我在每个平台上子类化ListViewRenderer并注册该事件.
在Android渲染器中,我然后调用Control.SmoothScrollToPositionFromTop(0,0)滚动到顶部.
在iOS渲染中,我调用Control.ScrollRectToVisible(new CoreGraphics.CGRect(0,0,1,1),true).
哇所有人都认为@Gareth Wynn,那个很酷的男人.无论如何这里是每个人都可以使用的代码,更改类名和命名空间,iOS不包括在内,只需使用Gareth Wynn的并行答案,就像Android一样:
共享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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
ANDROID 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3756 次 |
| 最近记录: |