Search code examples
androidlistviewandroid-listfragmentempty-listswiperefreshlayout

SwipeRefresh Layout not working with custom empty listview


I am trying to add a SwipeRefreshLayout layout in one of my fragment. Everything works as expected when the list contain some elements.

The problem is when my list is empty. I have a ViewStub showing up when the list doesn't contain any item, and at that point my OnRefreshListener is never triggered.

Here is my ListFragment xml:

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/list"
            android:fadingEdge="vertical"
            android:layout_gravity="center"
            android:cacheColorHint="@android:color/transparent"/>
        <ViewStub
            android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout="@layout/empty_renewal"/>
    </FrameLayout>

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

And the layout that I load in the ViewStub:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_warning"
            android:adjustViewBounds="true"
            android:layout_weight="1" />
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"/>
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/no_renewal"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/refresh_for_new"/>

</LinearLayout>

So is there a way I can get the swipe gesture to trigger the refresh when the list is empty?


Solution

  • Apparently writing the problem down helped.

    I solved my problem by encapsulating my ViewStub Layout in a ScrollView.

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center">
    
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"/>
                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_warning"
                    android:adjustViewBounds="true"
                    android:layout_weight="1" />
                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"/>
            </LinearLayout>
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/no_renewal"/>
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/refresh_for_new"/>
    
        </LinearLayout>
    </ScrollView>