Android SwipeRefreshLayout with empty TextView not working properly

ⅰ亾dé卋堺 提交于 2019-12-01 03:47:13
Phuc Tran

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.

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.

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>

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!