我有一个 Xamarin.Forms 应用程序 (XF 4.2.0.709249)。
我将 Scrollview 子类化并在 Android 项目中为它创建了一个渲染器。
我已经重写OnScrollChanged
了方法并连接了多个方法,scroll-changed-listeners
但是当滚动视图水平滚动时,它们都没有被调用。
我注意到适用于 Android的Xamarin.Forms ScrollViewRendererHorizontalScrollView
在内部使用 a进行水平滚动,这可能缺少调用事件/调用可覆盖方法的实现吗?
编辑:我还注意到,在渲染器ScrollTo(int x, int y)
方法中设置 x 值对 X-Scrolling 没有任何影响。ScrollX
渲染器内部永远保持 0,即使 XamarinForms Scrolled 事件发送更新的 X 坐标。
表格项目
public class FormsScrollView : ScrollView { }
public class SomePage : ContentPage {
public SomePage() {
//calls native scroll events
vertScrollView = new FormsScrollView() { Orientation = ScrollOrientation.Vertical, Content = SomeScrollableContent };
//never calls native scrollevents in renderer
horizontalScrollView = new FormsScrollView() { Orientation = ScrollOrientation.Horizontal };
horizontalScrollView.Content = CreateScrollContent(20, StackOrientation.Horizontal);
Content = horizontalScrollView;
}
private StackLayout CreateScrollContent(int amount, StackOrientation ori)
{
var sl = new StackLayout() { Orientation = ori };
for (int i = 0; i < amount; i++)
sl.Children.Add(new Label() { Text = "Cool Label " + i });
return sl;
}
}
安卓项目
[assembly: ExportRenderer(typeof(FormsScrollView), typeof(FormsScrollView_Droid))]
namespace Coolio.Droid.Renderers
{
public class FormsScrollView_Droid: ScrollViewRenderer, Android.Views.View.IOnScrollChangeListener
{
private readonly IOnScrollChangeListener _scrollListener;
public FormsScrollView_Droid(Context context) : base(context)
{
_scrollListener = new ScrollListener(this);
SetOnScrollChangeListener(this);
SetOnScrollChangeListener(_scrollListener);
this.ScrollX; //never changes, stays 0 forever
this.ScrollTo(x, y); //setting x has no effect, however setting y does scroll vertically correctly.
}
protected override void OnScrollChanged(int l, int t, int oldl, int oldt)
{
//never called on horizontal scroll
}
public void OnScrollChange(Android.Views.View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
{
//never called on horizontal scroll
}
}
public class ScrollListener : Java.Lang.Object, Android.Support.V4.Widget.NestedScrollView.IOnScrollChangeListener
{
private readonly FormsScrollView_Droid _scrolli;
public ScrollListener(FormsScrollView_Droid scrolli)
{
_scrolli = scrolli;
}
public void OnScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
{
//never called on horizontal scroll
}
}
}