From 6e74782409b6bf0c01a470db11543733f0213d54 Mon Sep 17 00:00:00 2001 From: pjo901018 Date: Sat, 24 Feb 2018 10:08:18 +0900 Subject: [PATCH] Implement onDispose / Merge ViewTreeObserver.OnScrollChangedListener into Listener #315 --- ...ViewScrollChangeEventObservableCompat.java | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/rxbinding/src/main/java/com/jakewharton/rxbinding2/view/ViewScrollChangeEventObservableCompat.java b/rxbinding/src/main/java/com/jakewharton/rxbinding2/view/ViewScrollChangeEventObservableCompat.java index 9f6b3b9d..66210b5f 100644 --- a/rxbinding/src/main/java/com/jakewharton/rxbinding2/view/ViewScrollChangeEventObservableCompat.java +++ b/rxbinding/src/main/java/com/jakewharton/rxbinding2/view/ViewScrollChangeEventObservableCompat.java @@ -30,23 +30,13 @@ protected void subscribeActual(Observer 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 observer; + private int oldl, oldt; Listener(View view, Observer observer) { this.view = view; @@ -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 { /**