From 20bcff59edaad3f4d5e58146a0c50366bce78928 Mon Sep 17 00:00:00 2001 From: Furkan VAROL Date: Tue, 3 Feb 2015 20:29:45 +0200 Subject: [PATCH] ObservableScrollView can now accepts more than one listener --- .../melnykov/fab/ObservableScrollView.java | 50 ++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/library/src/main/java/com/melnykov/fab/ObservableScrollView.java b/library/src/main/java/com/melnykov/fab/ObservableScrollView.java index 27a83f16..13687839 100644 --- a/library/src/main/java/com/melnykov/fab/ObservableScrollView.java +++ b/library/src/main/java/com/melnykov/fab/ObservableScrollView.java @@ -1,18 +1,37 @@ package com.melnykov.fab; - import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView; +import java.util.ArrayList; + +/** + * A custom {@link ScrollView} that can accept a scroll change listener. + */ public class ObservableScrollView extends ScrollView { + private ArrayList mCallbacks + = new ArrayList(); + /** + * {@link ObservableScrollView} scroll changed listener + */ public interface OnScrollChangedListener { + /** + * This is called in response to an internal scroll in this view (i.e., the + * view scrolled its own contents). This is typically as a result of + * {@link ScrollView#scrollBy(int, int)} or {@link ScrollView#scrollTo(int, int)} having been + * called. + * + * @param who owner of the event + * @param l Current horizontal scroll origin. + * @param t Current vertical scroll origin. + * @param oldl Previous horizontal scroll origin. + * @param oldt Previous vertical scroll origin. + */ void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt); } - private OnScrollChangedListener mOnScrollChangedListener; - public ObservableScrollView(Context context) { super(context); } @@ -28,12 +47,31 @@ public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) { @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); - if (mOnScrollChangedListener != null) { - mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt); + for (OnScrollChangedListener c : mCallbacks) { + c.onScrollChanged(this, l, t, oldl, oldt); } } + /** + * Adds given {@code listener} to callback list if not added before (not sets). + * + * @param listener {@link OnScrollChangedListener} + * + * @deprecated use {@link #addOnScrollChangedListener(OnScrollChangedListener)} instead + */ + @Deprecated public void setOnScrollChangedListener(OnScrollChangedListener listener) { - mOnScrollChangedListener = listener; + addOnScrollChangedListener(listener); + } + + /** + * Adds given {@code listener} to callback list if not added before. + * + * @param listener {@link OnScrollChangedListener} + */ + public void addOnScrollChangedListener(OnScrollChangedListener listener) { + if (!mCallbacks.contains(listener)) { + mCallbacks.add(listener); + } } } \ No newline at end of file