Skip to content

Instantly share code, notes, and snippets.

@grennis
Created May 11, 2016 23:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save grennis/16cb2b0c7f798418284dd2d754499b43 to your computer and use it in GitHub Desktop.
Save grennis/16cb2b0c7f798418284dd2d754499b43 to your computer and use it in GitHub Desktop.
This class allows easy use of SwipeRefreshLayout with RecyclerView (or ListView) and an "empty" view that can still be pulled to refresh
package com.innodroid.sample;
import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
//
// This class allows easy use of SwipeRefreshLayout with RecyclerView (or ListView)
// and an "empty" view that can still be pulled to refresh
//
// It expects a view layout such as:
//
// <SwipeRefreshLayoutWithEmpty ...>
// <FrameLayout ...>
// <TextView android:text="List is Empty" ...>
// <RecyclerView ...>
// </FrameLayout>
// </SwipeRefreshLayoutWithEmpty>
//
public class SwipeRefreshLayoutWithEmpty extends SwipeRefreshLayout {
private ViewGroup container;
public SwipeRefreshLayoutWithEmpty(Context context) {
super(context);
}
public SwipeRefreshLayoutWithEmpty(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean canChildScrollUp() {
// The swipe refresh layout has 2 children; the circle refresh indicator
// and the view container. The container is needed here
ViewGroup container = getContainer();
if (container == null) {
return false;
}
// The container has 2 children; the empty view and the scrollable view.
// Use whichever one is visible and test that it can scroll
View view = container.getChildAt(0);
if (view.getVisibility() != View.VISIBLE) {
view = container.getChildAt(1);
}
return ViewCompat.canScrollVertically(view, -1);
}
private ViewGroup getContainer() {
// Cache this view
if (container != null) {
return container;
}
// The container may not be the first view. Need to iterate to find it
for (int i=0; i<getChildCount(); i++) {
if (getChildAt(i) instanceof ViewGroup) {
container = (ViewGroup) getChildAt(i);
if (container.getChildCount() != 2) {
throw new RuntimeException("Container must have an empty view and content view");
}
break;
}
}
if (container == null) {
throw new RuntimeException("Container view not found");
}
return container;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment