Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@scana
Last active March 11, 2016 23:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scana/a5c0abc452abf202c9cf to your computer and use it in GitHub Desktop.
Save scana/a5c0abc452abf202c9cf to your computer and use it in GitHub Desktop.
RxAppBarLayout for observing AppBarLayout vertical offset
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