Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chayanforyou/43e7d04d0a36a5aa19e0bf51eb160510 to your computer and use it in GitHub Desktop.
Save chayanforyou/43e7d04d0a36a5aa19e0bf51eb160510 to your computer and use it in GitHub Desktop.
Fixing Up SwipeRefreshLayout to handle a collapsing toolbar
package au.com.qantas.qantas.common.presentation;
import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.util.AttributeSet;
import android.view.View;
import android.support.v4.widget.SwipeRefreshLayout;
public class AppbarSwipeRefreshLayout extends SwipeRefreshLayout implements
AppBarLayout.OnOffsetChangedListener {
private Boolean mCanRefresh = true;
private Boolean mAppbarExtended = true;
public AppbarSwipeRefreshLayout(Context context) {
super(context);
}
public AppbarSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
if (isEnabled()) {
// Dispatch up to the nested parent
dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dxConsumed, null);
} else {
dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null);
}
}
public void setCanRefresh(Boolean enabled) {
mCanRefresh = enabled;
setEnabled(mAppbarExtended && mCanRefresh);
}
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
mAppbarExtended = (verticalOffset == 0);
setEnabled(mAppbarExtended && mCanRefresh);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment