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 empty textview to the following working XML file:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listViewConversation"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1dp" />
</android.support.v4.widget.SwipeRefreshLayout>
Wrapping it in a Linear/Relative layout makes it buggy because the listview will always update when you want to slide back up the listview. One way I can think of is doing this programmatically but I guess that's not the best option.
You can learn how to implement it using this tutorial: Swipe to refresh GUIDE
So basically it all works fine but I would like to add an empty view that shows a message when the listview is empty.
Actually, the only think you are missing is having that empty
TextView
be wrapped with a scrollable container - for exampleScrollView
. 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 aScrollView
.empty.xml
Now in order to get rid of the famous
SwipeRefreshLayout
refresh-only-when-at-the-top issue, toggle theSwipeRefreshLayout
when necessary (Fragment-specific):That's it! Hope it helps! ;)
Btw, why would you use
SwipeRefreshLayout
withFrameLayout
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).Based on some answers here and the source code of SwipeRefreshLayout, I have subclassed the view to specifically handle having a RecyclerView (or ListView) and also an "empty" view inside a container which is the child.
It expects a layout such as
The code is:
Full gist: https://gist.github.com/grennis/16cb2b0c7f798418284dd2d754499b43
Why not wrap everything inside SwipeRefreshLayout
You may be just use NestedScrollView in SwipeRefreshLayout with single container. Below is the list of buggy use
mRecyclerView.setNestedScrollingEnabled(false);
Here's what I did : I disabled the swipe to refresh, unless my listView is up.
My Xml :
Works like a charm.
This worked for me