简体   繁体   中英

SwipeRefreshLayout not working with empty List

My app contains a SwipeRefreshLayout with a listView inside of it. The list receives data from a server. So when the data is fetched and the list is populated correctly the swipe to refresh works, but when the data is not passed to the list and is empty the SwipeRefresh doesn't work. Why can't I swipe when the listView is empty?

That's my layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorGrayHell"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="140dp"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:expandedTitleMarginBottom="56dp"
        app:contentScrim="@color/colorPrimary"
        app:expandedTitleGravity="bottom|center"
        app:expandedTitleTextAppearance="@style/CollBar"
        app:title="Tankstellen"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:toolbarId="@+id/toolbar">

        <ImageView
            android:id="@+id/backg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:src="@drawable/backthree"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_marginBottom="48dp"
            app:layout_collapseMode="pin"
            android:layout_gravity="bottom"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="55dp">

        <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="7dp"
                android:layout_marginRight="7dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/white"
                android:divider="@color/colorGrayHell"
                android:dividerHeight="10dp"
                android:nestedScrollingEnabled="true" />

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

    </RelativeLayout>

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

</android.support.design.widget.CoordinatorLayout>

Your listview and the swipe refresh layout is within a nested scroll view which means the height of child of the nested scroll view becomes wrap content (Relative layout in your case) automatically (It also might be showing you a lint warning to replace the match parent height of your relative layout to wrap content). therefore when there's no data in your list, the height of the whole layout group becomes 0 and the place where you need to swipe to refresh has a height of 0 and you cant swipe within a layout of height zero.

To solve this you can take a linear layout above your nested scroll view with the height of match parent and put your swipe refresh layout over it.

Let me know if you need the example code.

EDIT

Okay! so this is a workaround I found to solve the issue. You can try this and please let me know if this solves your problem. The Idea is to wrap everything a swipe refresh layout.

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">

  <android.support.v7.widget.LinearLayoutCompat
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<android.support.design.widget.CoordinatorLayout 
    android:layout_width="match_parent"
    android:background="@color/colorGrayHell"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:expandedTitleMarginBottom="56dp"
            app:contentScrim="@color/colorPrimary"
            app:expandedTitleGravity="bottom|center"
            app:expandedTitleTextAppearance="@style/CollBar"
            app:title="Tankstellen"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <ImageView
                android:id="@+id/backg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/backthree"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_marginBottom="48dp"
                app:layout_collapseMode="pin"
                android:layout_gravity="bottom"
                app:popupTheme="@style/AppTheme.PopupOverlay">

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="55dp">


                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="7dp"
                    android:layout_marginRight="7dp"
                    android:layout_marginTop="10dp"
                    android:background="@android:color/white"
                    android:divider="@color/colorGrayHell"
                    android:dividerHeight="10dp"
                    android:nestedScrollingEnabled="true" />


        </RelativeLayout>

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

    </android.support.design.widget.CoordinatorLayout>
  </android.support.v7.widget.LinearLayoutCompat>
   </android.support.v4.widget.SwipeRefreshLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM