6155031
6155031

Reputation: 4337

How to add pull to refresh in Coordinator layout

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.

Upvotes: 11

Views: 3952

Answers (5)

Memeusix
Memeusix

Reputation: 1

I was facing the same problem then I solved it by using programming

binding.appbar.addOnOffsetChangedListener { _, verticalOffset ->
        binding.swipeRefreshLayout.isEnabled = verticalOffset == 0
    }

Upvotes: 0

Iman Marashi
Iman Marashi

Reputation: 5753

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>

Upvotes: -1

arsent
arsent

Reputation: 7183

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
}) 

Upvotes: 19

6155031
6155031

Reputation: 4337

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);
        }
    });

Upvotes: 11

mohammadReza Abiri
mohammadReza Abiri

Reputation: 1799

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>

Upvotes: 9

Related Questions