Scrollview监听器在Xamarin for Android中无效?

我在Xamarin中使用C#来创建一个Android应用程序。 我创建了scrollview的扩展。 这是我的代码

public class EndlessScroll : ScrollView { public EndlessScroll (Context context) : base (context) { } public EndlessScroll(Context context, IAttributeSet attrs) : base(context, attrs) { } public EndlessScroll(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) { } public interface OnScrollViewListener { void onScrollChanged(EndlessScroll v, int l, int t, int oldl, int oldt); } public OnScrollViewListener scrollViewListener; public void setOnScrollViewListener(OnScrollViewListener scrollViewListener) { this.scrollViewListener = scrollViewListener; } protected void onScrollChanged(int l, int t, int oldl, int oldt) { base.OnScrollChanged(l, t, oldl, oldt); if (scrollViewListener != null) { scrollViewListener.onScrollChanged(this, l, t, oldl, oldt); } } } 

}

我试着将它实现到另一个类中。 我没有编译错误,但滚动视图触及底部时不会注册。 这是我在这里的代码。

 public class getFromParse:Activity, EndlessScroll.OnScrollViewListener { LinearLayout linear; Button buttonSort; Typeface font; protected override void OnCreate (Bundle bundle) { Window.RequestFeature(WindowFeatures.NoTitle); base.OnCreate (bundle); SetContentView (Resource.Layout.debugLog); EndlessScroll scroll = FindViewById  (Resource.Id.scrollView); scroll.setOnScrollViewListener (this); 

这里是滚动视图侦听器在它到达底部时应该检测到的位置。

 public void onScrollChanged(EndlessScroll scrollView, int x, int y, int oldx, int oldy) { // We take the last son in the scrollview View view = (View) scrollView.GetChildAt(scrollView.ChildCount - 1); int diff = (view.Bottom - (scrollView.Height + scrollView.ScrollY)); // if diff is zero, then the bottom has been reached if (diff == 0) { Console.WriteLine ("Scroll changed"); } } 

如果有人能帮助我,让我知道我做错了什么,那将是一个很大的帮助。 看来我做的一切都很正确,但我可能会遗漏一些东西。

我发现了我做错了什么。 如果将来有人需要在Xamarin中使用c#编写滚动监听器,我就是这样做的。

 public class EndlessScroll : ScrollView { private ScrollViewListener scrollViewListener = null; public EndlessScroll (Context context) : base (context) { } public EndlessScroll(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) { } public EndlessScroll(Context context, IAttributeSet attrs) : base(context, attrs) { } public interface ScrollViewListener { void OnScrollChanged(EndlessScroll v, int l, int t, int oldl, int oldt); } public void setOnScrollViewListener(ScrollViewListener scrollViewListener) { this.scrollViewListener = scrollViewListener; } protected override void OnScrollChanged(int l, int t, int oldl, int oldt) { base.OnScrollChanged (l, t, oldl, oldt); if (scrollViewListener != null) { scrollViewListener.OnScrollChanged (this, l, t, oldl, oldt); } } } 

如您所见,我忘记覆盖OnScrollChanged。

确保将接口实现到要使用它的活动中。然后将此代码放入oncreate中。

  EndlessScroll scroll = FindViewById  (Resource.Id.scrollView); scroll.setOnScrollViewListener (this); 

请务必添加以下方法,以便在更改滚动时进行注册。

 public void OnScrollChanged(EndlessScroll scrollView, int l, int t, int oldl, int oldt) { // We take the last son in the scrollview View view = (View) scrollView.GetChildAt(scrollView.ChildCount - 1); int diff = (view.Bottom - (scrollView.Height + scrollView.ScrollY)); // if diff is zero, then the bottom has been reached if (diff <= 0 && myResources.isLoading == false) { myResources.isLoading = true; //do stuff here } } 

正如您所看到的,只要没有加载内容,上面的代码就可以检测滚动到达底部的时间。