Last active
March 11, 2016 23:12
-
-
Save scana/a5c0abc452abf202c9cf to your computer and use it in GitHub Desktop.
RxAppBarLayout for observing AppBarLayout vertical offset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.design.widget.AppBarLayout; | |
import rx.Observable; | |
import rx.Subscriber; | |
import rx.Subscription; | |
/** | |
* Helper class for creating Observable of AppBarLayout visibility. | |
* Can be easily modified to return Integer values, if one needs to user verticalOffset param directly. | |
* See for further info: http://stackoverflow.com/questions/30779667/android-collapsingtoolbarlayout-and-swiperefreshlayout-get-stuck | |
*/ | |
public final class RxAppBarLayout { | |
public static Observable<Boolean> appBarVisibility(AppBarLayout appBarLayout) { | |
return Observable.create(new AppBarLayoutStateOnSubscribe(appBarLayout)); | |
} | |
private static class AppBarLayoutStateOnSubscribe implements Observable.OnSubscribe<Boolean> { | |
private final android.support.design.widget.AppBarLayout appBarLayout; | |
private Boolean mAppBarVisible = true; | |
private AppBarLayoutStateOnSubscribe(AppBarLayout appBarLayout) { | |
this.appBarLayout = appBarLayout; | |
} | |
@Override | |
public void call(Subscriber<? super Boolean> subscriber) { | |
AppBarLayout.OnOffsetChangedListener listener = new AppBarLayout.OnOffsetChangedListener() { | |
@Override | |
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { | |
mAppBarVisible = verticalOffset == 0; | |
subscriber.onNext(mAppBarVisible); | |
} | |
}; | |
appBarLayout.addOnOffsetChangedListener(listener); | |
subscriber.add(new Subscription() { | |
@Override | |
public void unsubscribe() { | |
appBarLayout.removeOnOffsetChangedListener(listener); | |
} | |
@Override | |
public boolean isUnsubscribed() { | |
return false; | |
} | |
}); | |
subscriber.onNext(mAppBarVisible); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment