Skip to content

Instantly share code, notes, and snippets.

@thuytrinh
Last active August 25, 2016 04:39
Show Gist options
  • Select an option

  • Save thuytrinh/b817ec3139ef65be60d6 to your computer and use it in GitHub Desktop.

Select an option

Save thuytrinh/b817ec3139ef65be60d6 to your computer and use it in GitHub Desktop.
A custom SwipeRefreshLayout that fixes the scroll-up issue with StickyListHeadersListView
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
public class CustomSwipeRefreshLayout extends SwipeRefreshLayout {
/**
* A StickyListHeadersListView whose parent view should be this SwipeRefreshLayout
*/
private StickyListHeadersListView mTargetView;
public CustomSwipeRefreshLayout(Context context) {
super(context);
}
public CustomSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setTargetView(StickyListHeadersListView targetView) {
mTargetView = targetView;
}
@Override
public boolean canChildScrollUp() {
if (mTargetView != null) {
// In order to scroll a StickyListHeadersListView up:
// Firstly, the wrapped ListView must have at least one item
return (mTargetView.getListChildCount() > 0) &&
// And then, the first visible item must not be the first item
((mTargetView.getFirstVisiblePosition() > 0) ||
// If the first visible item is the first item,
// (we've reached the first item)
// make sure that its top must not cross over the padding top of the wrapped ListView
(mTargetView.getListChildAt(0).getTop() < 0));
// If the wrapped ListView is empty or,
// the first item is located below the padding top of the wrapped ListView,
// we can allow performing refreshing now
} else {
// Fall back to default implementation
return super.canChildScrollUp();
}
}
}
@thuytrinh
Copy link
Copy Markdown
Author

Based on an example code from StackOverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment