Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObservableScrollView can now accepts more than one listener #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions library/src/main/java/com/melnykov/fab/ObservableScrollView.java
Original file line number Diff line number Diff line change
@@ -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<OnScrollChangedListener> mCallbacks
= new ArrayList<OnScrollChangedListener>();

/**
* {@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);
}
Expand All @@ -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);
}
}
}