1

I'm currently developing for Android using C# and Xamarin.

I want to add a listener to the scroll views that I have within my app, so that it picks up when a user is touching the scroll bar.

How can I go about this?

Thanks!

bamb094
  • 19
  • 1
  • 3

1 Answers1

0
  • Refer to this , in xamarin.android usage like this

    public class MainActivity : Activity, IOnScrollChangedListener
    {
    ScrollView msv;
    
    public void OnScrollChanged()
    {
        float scrollY = msv.ScrollX;
        float scrollX = msv.ScrollY;
        System.Diagnostics.Debug.Write("x:=" + scrollY + "  y=" + scrollX );
        //do something
    }
    
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        msv = FindViewById<ScrollView>(Resource.Id.sv);
        msv.ViewTreeObserver.AddOnScrollChangedListener(this);
    
    }
    }
    
  • you can also write up a class MyScrollView to extend ScrollView,and then override OnTouchEvent or onScrollChanged(int x, int y, int oldX, int oldY)method to implement your function.

Robbit
  • 4,300
  • 1
  • 13
  • 29
  • Thanks very much! I managed to get this working. However, I am using analytics to track all events within my app, so in this case I will track when the scroll is clicked. I have just tested it and the count for the scroll bar is +115, can you help with this? Thanks again! – bamb094 Nov 30 '17 at 10:11
  • You mean you are using `Google Analytics` to track events , and you want to track the click event of the `ScrollView` not scroll event? – Robbit Dec 01 '17 at 09:00
  • I want to check when the user has tapped on a scroll bar - I hope this makes sense :) – bamb094 Dec 01 '17 at 10:12