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.
Put SwipeRefreshLayout into a FrameLayout and other views behind it.
This was a very frustrating issue for me but after a few hours with try and fail I came up with this solution.
With this I can refresh even with empty view visible (and RecyclerView too of course)
In my layout file I have this structure:
In code:
I have tried following thing. In both empty view, and case of a list, swipe will refresh the data, also fastscroll is working fine without any code change required in activity. I have put emptyview before the listview and marked its visiblity to gone. and listview is put after that inside SwipeToRefresh block. Sample Code -
I didn't liked the limitation to a single child. Furthermore the current implementation of the SwipeRefreshLayout has an hardcoded "magic" handling for ScrollView, ListView and GridView that trigger only if the view it's the direct child of your own view.
That said the good news it's that it is open source, so you can either copy the code and adapt to your needs or you can do what I did:
Use two DIFFERENT SwipeRefreshLayout, one for the Empty view and one for the ListView.
Then tell your listview that the empty list view is the swipe refresh layout of the empty view.
Now the empty refresh layout will be automatically hidden by your list view when you have data and will be shown when the list is empty.
The swipe refresh layout of the list shouldn't receive touch events cause the list is hidden.
Good luck.
Another option that works nicely in case that your empty view doesn't need any interaction. For example, it is a simple textview saying "No data in here."
This puts the empty view behind the SwipeToRefreshLayout which is transparent and which contains also a transparent RecyclerView.
Then, in the code, in the place where you add the items to the recycler view adapter, you check if the adapter is empty, and if so you set the visibility of the empty view to "visible". And vice versa.
The trick is that the view is behind the transparent recycler view, which means that the recycler view and his parent, the SwipeToRefreshLayout, will handle the scroll when there are no items in the recycler. The empty view behind won't even be touched so the touch event will be consumed by the SwipeTorefreshLayout.
The custom RecyclerSwipeTorefreshLayout should handle the canChildScrollUp method in the following way
This will do the trick.
UPDATE: Of course, the recycler view doesn't have to be transparent all the time. You can update the transparency to be active only when the adapter is empty.
Cheers!
There is no need for any workaround.
You can simply use this view hierarchy :
Then, in code, you just call:
That's it.
EDIT: If you wish to be able to swipe-to-refresh even when the empty view is shown, you will have to somehow avoid hiding the ListView, so you could use a customized ListView that has this function inside:
Together with the solution I wrote, the swipe-to-refresh is shown no matter how many items you are showing.
Of course, if you really want to hide the ListView, you should change the code. Maybe add "setVisibilityForReal(...)" :)