Android SwipeRefreshLayout with empty TextView not working properly
Asked Answered
L

4

9

What i want to do is , allow user to swipe and refresh the list, it does not matter if the data is there or not. But when data is there in the listview only the listview should be visible, and when data is not there , Empty textview should be visible. In both the cases user must be able to refresh list by swipe.

I tried some solution given here in this discussion but none of them sounds working, the idea of taking two SwipeToRefresh works fine as given Here, but it shows empty container even while fetching data from server.

I tried it with my own logic wrapping Listview and Textview inside a Relative/FrameLayout but SwipeToRefresh accepts one view only as per its behavior

Here is an xml snippet i have tried

 <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/activity_main_swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.945" >


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <ListView
                android:id="@+id/event_list_eventlist"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:divider="@color/event_background"
                android:dividerHeight="1dp" >
            </ListView>

            <TextView
                android:id="@+id/event_txt_nothing_found"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerInParent="true"
                android:layout_centerVertical="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/darker_gray"
                android:visibility="gone" />
        </RelativeLayout>
    </android.support.v4.widget.SwipeRefreshLayout>

Please help

Lim answered 13/6, 2015 at 11:22 Comment(0)
F
13

I solved same problem by using FrameLayout, look at below code:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/layout_swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ListView
            android:id="@+id/lv_products"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@null" />

    </android.support.v4.widget.SwipeRefreshLayout>

    <TextView
        android:id="@+id/tv_no_product"
        style="@android:style/TextAppearance.Medium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:gravity="center"
        android:text="@string/msg_no_listing"
        android:textColor="@color/primary_green"
        android:visibility="gone" />
</FrameLayout>

You could use RelativeLayout as well, but the key is placing your ListView inside the SwipeRefreshLayout, and the TextView outside.

Fingered answered 13/6, 2015 at 11:43 Comment(13)
and what to do in activity? i mean, does this implementation allow swipe again if the list is empty?Lim
Sure, you can swipe again if the list is empty. I'm sure it works 100%.Fingered
can you please put a bit of code for activity if possible?Lim
Did you try above layout?Fingered
yes i am trying but this code is none of use without java code, please if you can help me in it,Lim
Can you share your activity?Fingered
Let us continue this discussion in chat.Lim
The problem is that if you set tv_no_product as emptyView for the ListView, then the ListView sets its visibility to GONE while the emptyView is visible, and you can't swipe to refresh when the ListView is GONE.Scapular
No need to set ListView's visibility go GONE. Because if list view is empty, it shows nothing but it's still there.Fingered
As @Scapular says with that solution, you can't refresh the empty view. Solution is to put the FrameLayout (that includes the ListView and the EmptyView) inside the SwipeRefreshLayout. Also, set android:clickable=”true” in the FrameLayoutConcepcionconcept
@Concepcionconcept The solution is a bit more complex than that, for it to work properly on Android 2.x you need to override SwipeRefreshLayout.canChildScrollUp()Scapular
i tried with this but unfortunately not working for meIdealistic
Can you upload your layout somewhere and send me the link?Fingered
B
12

I had this issue too, and solved it without any additional code in the activity by using the layout below.

If you are using a ListActivity or ListFragment it handles showing/hiding the empty view for you, and refreshing works with an empty list as well with this layout structure.

No hacks needed, everything is in the SwipeRefreshLayout, as it should be.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Refresher"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@null" />
        <ScrollView
            android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:text="Geen formulieren gevonden"
                style="@style/text.EmptyView" />
        </ScrollView>
    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>

Hope it helps anyone struggling with this issue.

Bufordbug answered 16/7, 2015 at 8:21 Comment(1)
Great, it works fine. I'm wondering why? is there any explanation for this?Permafrost
V
2

My issue seems to be undocumented, but if the adapter for a recycler view has no data, and you have called the setLayoutManager function, then the recycler view becomes un-swipeable. My solution was to only call setLayoutManager after some data had been received.

Voyles answered 5/2, 2017 at 17:2 Comment(0)
I
0

finally solve the issue using following code

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_container"
        android:layout_below="@+id/search_product"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_shop_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <ScrollView
                android:gravity="center"
                android:fillViewport="true"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <com.forysta.inloyal.view.textviews.RegularTextView
                    android:id="@+id/tv_no_shop_data"
                    style="@style/text_Size_16"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="@string/alert_nodatashop"
                    android:visibility="visible"
                    app:layout_collapseMode="parallax" />
            </ScrollView>
        </LinearLayout>
    </android.support.v4.widget.SwipeRefreshLayout>
Idealistic answered 13/10, 2016 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.