在Xamarin的Android中,ScrollView监听器无法工作?

4
我正在使用C#和Xamarin创建一个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 <EndlessScroll> (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"); 
        }
    }

如果有人能帮我并让我知道我做错了什么,那将是一大帮助。看起来我做的一切都是对的,但可能有些遗漏。

我知道你已经找到了解决方案,但是你尝试过这个控件吗? https://github.com/XLabs/Xamarin-Forms-Labs/wiki/CarouselView - kyurkchyan
1个回答

8

我发现了我做错的事情。如果将来有人需要在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 <EndlessScroll> (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 
        }


    }

如您所见,上述代码可以在没有内容加载的情况下检测滚动何时到达底部。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接