Xamarin android C# ScrollView OnScrollChanged event -
in xamarin android how can extend scrollview in order use protected onscrollchanged event?
specifically, how extend scrollview allow eventhandler registered onscrollchanged event? other methods of scrollview need implemented in class extends scrollview?
reasons:
the android scrollview not have way listen scroll event. there have been various questions regarding how extend scrollview in native android java, however, there not question , answer addressing how apply xamarin.
in order extend scrollview in way should implement 3 constructors
public usefulscrollview(context context) public usefulscrollview(context context, iattributeset attrs) public usefulscrollview(context context, iattributeset attrs, int defstyle)
we need override ondraw method
protected override void ondraw(android.graphics.canvas canvas)
to achieve functionality of event can respond when user scrolls need override onscrollchanged method.
protected override void onscrollchanged(int l, int t, int oldl, int oldt)
there multiple ways allow event listening , handling, in order consistent xamarin can add public eventhandler property our class.
public eventhandler<t> scrolleventhandler { get; set; }
we want pass along values onscrollchanged eventhandler, let's extend eventargs
public class usefulscrolleventargs : eventargs{ public int l { get; set; } public int t { get; set; } public int oldl { get; set; } public int oldt { get; set; } }
finally, don't forget initialize our handler in each of our constructors
scrolleventhandler = (object sender, usefulscrolleventargs e) => {};
put , might this
extended eventargs class
public class usefulscrolleventargs : eventargs{ public int l { get; set; } public int t { get; set; } public int oldl { get; set; } public int oldt { get; set; } }
extended scrollview class
public class usefulscrollview : scrollview { public eventhandler<usefulscrolleventargs> scrolleventhandler { get; set; } public usefulscrollview (context context) : base(context) { scrolleventhandler = (object sender, usefulscrolleventargs e) => {}; } public usefulscrollview (context context, iattributeset attrs) : base(context, attrs) { scrolleventhandler = (object sender, usefulscrolleventargs e) => {}; } public usefulscrollview(context context, iattributeset attrs, int defstyle) : base(context, attrs, defstyle) { scrolleventhandler = (object sender, usefulscrolleventargs e) => {}; } protected override void onscrollchanged(int l, int t, int oldl, int oldt) { scrolleventhandler (this, new usefulscrolleventargs () {l=l,t=t,oldl=oldl,oldt=oldt}); base.onscrollchanged (l, t, oldl, oldt); } protected override void ondraw(android.graphics.canvas canvas) { } }
this q&a helpful in figuring problem out: scrollview listener not working in xamarin android?
Comments
Post a Comment