Last active
August 8, 2018 07:50
-
-
Save dineshmm23/907f45ff1d600159f7d45f02f01949c2 to your computer and use it in GitHub Desktop.
A couple of common usage patterns on Android is to use the SwipeRefreshLayout to enable pull-to-refresh, and also to have a custom “empty” view that displays when there are no items in the list. Unfortunately trying to combine these two patterns often results in problems. Reference--> http://innodroid.com/blog/post/recycler-empty-swipe-refresh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<SwipeRefreshLayoutWithEmpty ...> | |
<FrameLayout ...> | |
<TextView android:text="List is Empty" ...> | |
<RecyclerView ...> | |
</FrameLayout> | |
</SwipeRefreshLayoutWithEmpty> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
if (container.getChildCount() != 2) { | |
throw new RuntimeException("Container must have an empty view and content 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); | |
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