Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollview listener is not working in Xamarin for Android?

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

    }


}

}

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.

like image 237
Kirasoft Avatar asked Feb 11 '23 17:02

Kirasoft


1 Answers

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


}

As you can see I forgot to override the OnScrollChanged.

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 
        }


    }

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.

like image 140
Kirasoft Avatar answered Feb 15 '23 09:02

Kirasoft