javaandroidandroid-appbarlayoutswiperefreshlayoutmaterial3

Weird AppBarLayout and SwipeRefreshLayout Behavior in Material 3


I'm creating an app that has swipe-to-refresh feature with Material 3 (Material You) design. But when scrolling the content under the SwipeRefreshLayout, the app bar doesn't lift properly as expected. It works correctly when the SwipeRefreshLayout is refreshing.

activity-main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:fitsSystemWindows="true"

    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:fitsSystemWindows="true"
        app:liftOnScroll="true">

        <com.google.android.material.appbar.MaterialToolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:title="App Bar and Swipe Layout"
            app:titleCentered="true"/>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:text="Long lorem ipsum text..."/>

        </androidx.core.widget.NestedScrollView>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

MainActivity.java

package com.fourbits.appbarandswiperefresh;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

I tried to change my SwipeRefreshLayout androidx dependency version from 1.2.0-alpha01 to 1.1.0, but the problem still exists.

I expect that the app bar will lift properly when the content is scrolled under the SwipeRefreshLayout.


Solution

  • Using app:liftOnScrollTargetViewId="@id/nestedScroll" on app bar layout solves it.

    Reference: https://github.com/material-components/material-components-android/issues/2825