11

I have an AppBar and RecyclerView in CoordiantorLayout. SwipeToRefresh has to be fullscreen but RecyclerView not scrolling down then.

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="128dp"/>

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

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


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


</androidx.coordinatorlayout.widget.CoordinatorLayout>

oid

How to add fullscreen pull to refresh in Coordinator layout without a library.

aminography
  • 21,986
  • 13
  • 70
  • 74
6155031
  • 4,171
  • 6
  • 27
  • 56

4 Answers4

18

You can disable swipe refresh layout when AppBar isn't fully expanded and enable it if it is fully expanded.

vAppBar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { _, verticalOffset ->
   vSwipeRefresh.isEnabled = verticalOffset == 0
}) 
arsent
  • 6,975
  • 3
  • 32
  • 31
11

I add swipetorefresh top level like answer above. And I fix my scroll up issue with code below. Thanks to mohammadReza :)

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            int topRowVerticalPosition =
                    (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
            swipeRefreshLayout.setEnabled(topRowVerticalPosition >= 0);

        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }
    });
6155031
  • 4,171
  • 6
  • 27
  • 56
9

try to set SwipeRefreshLayout as root parent like this :

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

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />


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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="128dp" />

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


    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
mohammadReza Abiri
  • 1,759
  • 1
  • 9
  • 20
-1

To use SwipeRefreshLayout with a CoordinatorLayout and FloatingActionButton (FAB), you can follow these steps:

Update your layout XML:

Open the XML layout file where you want to add the components. Include the <androidx.swiperefreshlayout.widget.SwipeRefreshLayout> element as the parent layout. Inside the SwipeRefreshLayout, include the CoordinatorLayout and other views. Here's an example:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        app:popupTheme="@style/ThemeOverlay.MaterialComponents.Light"
        app:title="My Toolbar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_anchor="@id/toolbar"
        app:layout_anchorGravity="bottom" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_scroll_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_arrow_upward"
        app:layout_anchor="@id/recycler_view"
        app:layout_anchorGravity="bottom|end"
        app:layout_margin="@dimen/fab_margin" />

</CoordinatorLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
Iman Marashi
  • 5,593
  • 38
  • 51