10

I am trying to implement pull to refresh but I'm having an issue with SwipeRefreshLayout not wrapping the child view's height. In the view preview and in a live build it appears to have 0 height.

The layout as as follows:

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="0dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:layout_collapseMode="pin">

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

            <include layout="@layout/child_layout" />

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

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

I have also tried making the SwipeRefreshLayout the parent of the AppBarLayout without any success as well as putting a singular LinearLayout inside of the SwipeRefreshLayout. The only thing that seems to prevent the height of the swipe layout from being 0 is to set it statically but I want it to be dynamic based upon the height of the child view.

Is there something I'm missing here? It seems like there may be a bug with SwipeRefreshLayout because replacing it with a LinearLayout that also wraps the content works as expected.

kira_codes
  • 1,457
  • 13
  • 38

3 Answers3

6

The problem is your SwipeRefreshLayout is inside the Toolbar and AppBarLayout. You must wrap AppBarLayout with another layout and put SwipeRefreshLayout below the AppBarLayout. An example is at the below.

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:fitsSystemWindows="true"
tools:context="com.vsahin.moneycim.View.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:fitsSystemWindows="true"
    app:expanded="false">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        android:background="@drawable/gradient_background">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|enterAlways" />

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

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

<android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

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

</android.support.design.widget.CoordinatorLayout>
VolkanSahin45
  • 1,922
  • 12
  • 25
  • I forgot to include in my original post but there is a Recycler View with bottom sheet below the App Bar Layout. The issue is that I need the app bar layout for the parallax effect when the bottom sheet is opened but I only want to support pull to refresh on the app bar layout. – kira_codes Jan 24 '18 at 23:02
  • Why you are fixing the AppBarLayout's height, it will not work if you change it to wrap_content – Safeer Mar 13 '18 at 13:04
  • I want to do that because in my original code I put a fragment there. In my fragment I am using auto scale textview that fit in bounds. @Safeer – VolkanSahin45 Mar 13 '18 at 14:01
  • I think the problem is collapsing toolbar is match parent and if you do apparlayout wrap content layout cant draw layout. appbar will try to wrap content but collapsingtoolbar is match parent and it will try to match its parents but appbar dont have a fix size. @Safeer – VolkanSahin45 Mar 13 '18 at 14:05
  • I got caught up working on other things and just came back to this, but you were right. This works as expected. The reason why it initially didn't work for me was because I forgot about some programatic behavior logic that just needed to be updated. Thanks! – kira_codes Apr 12 '18 at 18:56
1

if your SwipeRefreshLayout height is wrapping the content of your child_layout, then your child_layoutheight should be set to match_parent OR both SwipeRefreshLayout and child_layout height should be set to match_parent as shown in the Android Documentation

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
      <!--Child View-->
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.v4.widget.SwipeRefreshLayout>
Frank Odoom
  • 1,545
  • 18
  • 19
-1

SwipeRefreshLayout is a transparent View, so best solution to set it match_parent it will not conflict with other Views and don't include View or Layout within it, keep it clean

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

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="0dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:layout_collapseMode="pin">

        <include layout="@layout/child_layout" />

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

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

SwipeRefreshLayout has to be in top to overlay other layers and be clickable

Mohamed Embaby
  • 960
  • 8
  • 26