Skip to content

Instantly share code, notes, and snippets.

@ian-ellis
Last active October 9, 2022 09:36
Show Gist options
  • Save ian-ellis/cba266b51910f75e9b00 to your computer and use it in GitHub Desktop.
Save ian-ellis/cba266b51910f75e9b00 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