滚动型侦听器未在Xamarin合作为Android? [英] Scrollview listener is not working in Xamarin for Android?

查看:185
本文介绍了滚动型侦听器未在Xamarin合作为Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xamarin使用C#创建一个Android应用程序。我创建了滚动型的延伸。这里是我的code为

I am using for C# in Xamarin to create an android app. I have created an extension of the scrollview. Here is my code for that

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);
          }

    }


}

}

我想实现它到另一个类。我没有得到编译错误,但是当滚动视图已触底,将不能注册。这里是code我这里有。

I tried to implement it into another class. I am getting no compile errors but it will not register when the scrollview has hit the bottom. Here is the code I have here.

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); 

和这里是滚动视图监听器应该当它击中底部检测。

And here is where the scroll view listener is supposed to detect when it hits the bottom.

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"); 
        }
    }

如果有人可以帮助我,让我知道我做错了,这将是一个很大的帮助。看来,我做的一切权利,但我可能失去了一些东西。

If anybody could help me out and let me know what I am doing wrong, that would be a great help. It seems that I am doing everything right, but I may be missing something.

推荐答案

我发现我在做什么错。在任何情况下,在未来需要编写使用C#在Xamarin滚动听众,这里是我做到了。

I found out what I was doing wrong. In case anybody in the future needs to write a scroll listener using c# in Xamarin, here is how I did it.

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。

As you can see I forgot to override the OnScrollChanged.

请你确定你实现接口转换成你想使用它的活动,然后把这个code在OnCreate中。

Make you sure you implement the interface into the activity you want to use it in. Then put this code in your oncreate.

 EndlessScroll  scroll = FindViewById <EndlessScroll> (Resource.Id.scrollView); 


        scroll.setOnScrollViewListener (this); 

一定要添加下面的方法,使得当滚动改变它可以注册。

Be sure to add the method below so that it can register when the scroll is changed.

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 
        }


    }

正如你可以看到上面的code可让当滚动已经只要没有内容加载到了谷底它来检测。

As you can see the above code allows it to detect when the scroll has reached the bottom as long as no content is loading.

这篇关于滚动型侦听器未在Xamarin合作为Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆