Toolbar not showing with swipe to refresh

5.2k views Asked by At

I'm trying to implement collapsing tollbar with swipe to refresh and recyclerview. When I'm trying to scroll (when recyclerview has only one item) toolbar collapse, but when I'm trying to scroll down to show toolbar, it's impossible because swipe down causes swipe to refresh. When recyclerview has more item it works perfectly.

Link to gif with problem

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="1dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="1dp"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways" />


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

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/activity_main_swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/cities_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="16dp"
    android:elevation="1dp"
    android:onClick="addCity"
    android:src="@drawable/ic_plus_white_36dp"
    app:borderWidth="0dp" />

2

There are 2 answers

2
Ahmad Shahriari On BEST ANSWER

Update: This bug has been fixed in the version 23.1.1 of support library

You can set onOffsetChanged listener for your AppBarLayout and prevent to swipe refreshing until AppBarLayout layout offset 0.

This is good example : https://gist.github.com/blackcj/001a90c7775765ad5212

0
hkop On

I managed it by adding the following implementation of OnOffsetChangedListener in fragment:

@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
    if (collapsingToolbarLayout.getHeight() + verticalOffset < 2 * ViewCompat.getMinimumHeight(collapsingToolbarLayout)) {
        swipeRefreshLayout.setEnabled(false);
    } else {
        swipeRefreshLayout.setEnabled(true);
    }
}

@Override
public void onResume() {
    super.onResume();
    appBarLayout.addOnOffsetChangedListener(this);
}

@Override
public void onPause() {
    super.onPause();
    appBarLayout.removeOnOffsetChangedListener(this);
}