I\'ve implemented SwipeRefreshLayout
into my app but it can only hold one direct child which should be the listview. I\'m trying to figure out how to add an emp
Actually, the only think you are missing is having that empty TextView
be wrapped with a scrollable container - for example ScrollView
. For details, have a look at SwipeRefreshLayout.canChildScrollUp() method and its usage.
Anyway, back to the point. Here is a successful implementation:
activity_some.xml
Where your empty.xml
is basically anything you wish wrapped with a ScrollView
.
empty.xml
Now in order to get rid of the famous SwipeRefreshLayout
refresh-only-when-at-the-top issue, toggle the SwipeRefreshLayout
when necessary (Fragment-specific):
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
@Override
public void onStart() {
super.onStart();
mOnScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
int scrollY = mWebView.getScrollY();
if (scrollY == 0)
swipeLayout.setEnabled(true);
else
swipeLayout.setEnabled(false);
}
};
swipeLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener);
}
@Override
public void onStop() {
swipeLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
super.onStop();
}
That's it! Hope it helps! ;)
Btw, why would you use SwipeRefreshLayout
with FrameLayout
this way? Because this way you can do smooth transition animations, like crossfade effects, and any of your state views can be swipeable (in case you want a unified fetch/refresh/retry mechanism).