2014-12-16 2 views
1

Xamarin 안드로이드에서 보호 된 OnScrollChanged 이벤트를 사용하기 위해 어떻게 ScrollView를 확장 할 수 있습니까?Xamarin android C# ScrollView OnScrollChanged 이벤트

특히, OnScrollChanged 이벤트에 EventHandler를 등록 할 수 있도록 ScrollView를 확장하는 방법은 무엇입니까? ScrollView를 확장하는 클래스에서 ScrollView의 다른 메소드를 구현해야합니까?

이유 :

안드로이드있는 ScrollView는 스크롤 이벤트를 수신 할 수있는 방법이 없습니다. 기본 Android Java에서 ScrollView를 확장하는 방법에 대한 다양한 질문이 있지만 Xamarin에이 방법을 적용하는 방법에 대한 질문과 대답은 없습니다. 우리는 또한 우리가 응답 할 수있는 이벤트의 기능을 달성하기 위해의 OnDraw 방법

protected override void OnDraw(Android.Graphics.Canvas canvas) 

를 재정의해야 우리가 3 생성자

public UsefulScrollView(Context context) 
public UsefulScrollView(Context context, IAttributeSet attrs) 
public UsefulScrollView(Context context, IAttributeSet attrs, int defStyle) 

를 구현해야 이런 식으로있는 ScrollView를 확장하기 위해

답변

3

사용자가 스크롤 할 때까지 OnScrollChanged 메서드를 재정의해야합니다.

protected override void OnScrollChanged(int l, int t, int oldl, int oldt) 

이벤트가 수신 및 처리, 그러나 우리는 우리의 클래스에 공공 이벤트 핸들러 속성을 추가 할 수있는 자 마린과 일치하기 위해 수 있도록 여러 가지 방법이 있습니다.

public EventHandler<T> ScrollEventHandler { get; set; } 

우리는 우리의 생성자의 각 우리의 핸들러를 초기화하는 것을 잊지 마십시오 핸들러에 OnScrollChanged에서 값을 따라 전달하려면, 그래서 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; } 
} 

을 확장 할 것

ScrollEventHandler = (object sender, UsefulScrollEventArgs e) => {}; 

함께 넣으면 다음과 같이 보일 수 있습니다.

확장있는 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; } 
} 

확장있는 ScrollView 클래스

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

    } 
} 

이 Q & A는이 문제를 파악하는데 도움이되었다 : 내가있는 ScrollView를 확장하지만 EventHandlers을 사용 Scrollview listener is not working in Xamarin for Android?

1

. ScrollChanged에 하나, ReachedBottom에 하나.

이 확장 된 클래스에 간다 : 당신은 단순히

myScrollView.ScrollChanged += delegate { /* your code here */ } 

myScrollView.ScrollReachedBottom += delegate { /* your code here */ } 

즐길 부를 것이다

public delegate void ScrollChangedEventHandler(object sender, EventArgs e); 
    public event ScrollChangedEventHandler ScrollChanged; 

    public delegate void ScrollReachedBottomEventHandler(object sender, EventArgs e); 
    public event ScrollReachedBottomEventHandler ScrollReachedBottom; 

    protected override void OnScrollChanged(int l, int t, int oldl, int oldt) 
    { 
     base.OnScrollChanged(l, t, oldl, oldt); 

     if (ScrollChanged != null) 
     { 
      ScrollChanged.Invoke(this, new EventArgs()); 
     } 

     // Grab the last child placed in the ScrollView, we need it to determinate the bottom position. 
     var view = GetChildAt(ChildCount - 1); 

     // Calculate the scrolldiff 
     var diff = (view.Bottom - (Height + ScrollY)); 

     // if diff is zero, then the bottom has been reached 
     if (diff == 0 && ScrollReachedBottom != null) 
     { 
      ScrollReachedBottom.Invoke(this, new EventArgs()); 
     } 
    } 

.