Skip to content

Instantly share code, notes, and snippets.

@scana scana/java
Created Nov 16, 2015

Embed
What would you like to do?
RxAppBarLayout
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 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
You can’t perform that action at this time.