水平滚动时未调用 OnScrollChange(...)

OnScrollChange(...) not called on horizontal scroll

我有一个 Xamarin.Forms 应用程序 (XF 4.2.0.709249)。

我已经将 Scrollview 子类化并在 Android 项目中为它创建了一个渲染器。

我已经覆盖了 OnScrollChanged 方法并连接到多个 scroll-changed-listeners,但是当水平滚动滚动视图时 none 会调用其中的

我注意到 Android 的 Xamarin.Forms ScrollViewRenderer 在内部使用 HorizontalScrollView 进行水平滚动,这可能缺少调用 events/call 可覆盖方法的实现吗?

编辑:我还注意到,在渲染器 ScrollTo(int x, int y) 方法中设置 x 值对 X 滚动没有任何影响。 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;
    }
}

Android 项目

[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
        }
    }
}

参考这个话题: https://forums.xamarin.com/discussion/62241/problem-setting-on-scroll-listener-to-android-horizontal-scroll-view-class-not-found-exception

我更改了 FormsScrollView_Droid 如下代码。

 public class FormsScrollView_Droid: ScrollViewRenderer, ViewTreeObserver.IOnScrollChangedListener
{
   // private readonly IOnScrollChangeListener _scrollListener;

public FormsScrollView_Droid(Context context) : base(context)
{

        ViewTreeObserver.AddOnScrollChangedListener(this);

   // SetOnScrollChangeListener(_scrollListener);
}


protected override void OnScrollChanged(int l, int t, int oldl, int oldt)
{
    //called on Vertical scroll
}

public void OnScrollChange(Android.Views.View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
{

}

  public void OnScrollChanged()
  {
       //called on Horizontal scroll
  }
}

这是运行 GIF。