Skip to content

Commit

Permalink
Implement onDispose / Merge ViewTreeObserver.OnScrollChangedListener …
Browse files Browse the repository at this point in the history
…into Listener JakeWharton#315
  • Loading branch information
karrot-farmer committed Feb 24, 2018
1 parent 5ea04fc commit 6e74782
Showing 1 changed file with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,13 @@ protected void subscribeActual(Observer<? super ViewScrollChangeEvent> observer)

private void setOnScrollChangeListenerWith(final View v, final Listener listener) {
ViewTreeObserver viewTreeObserver = v.getViewTreeObserver();
viewTreeObserver.addOnScrollChangedListener(
new ViewTreeObserver.OnScrollChangedListener() {
private int oldl, oldt;

@Override
public void onScrollChanged() {
listener.onScrollChange(v, v.getScrollX(), v.getScrollY(), oldl, oldt);
oldl = v.getScrollX();
oldt = v.getScrollY();
}
}
);
viewTreeObserver.addOnScrollChangedListener(listener);
}

static final class Listener extends MainThreadDisposable implements OnScrollChangeListener {
static final class Listener extends MainThreadDisposable implements OnScrollChangeListener, ViewTreeObserver.OnScrollChangedListener {
private final View view;
private final Observer<? super ViewScrollChangeEvent> observer;
private int oldl, oldt;

Listener(View view, Observer<? super ViewScrollChangeEvent> observer) {
this.view = view;
Expand All @@ -61,9 +51,17 @@ public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int
}

@Override protected void onDispose() {
view.setOnClickListener(null);
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
viewTreeObserver.removeOnScrollChangedListener(this);
}

@Override
public void onScrollChanged() {
this.onScrollChange(view, view.getScrollX(), view.getScrollY(), oldl, oldt);
oldl = view.getScrollX();
oldt = view.getScrollY();
}
}
}

public interface OnScrollChangeListener {
/**
Expand Down

0 comments on commit 6e74782

Please sign in to comment.