Problems when SwipeRefreshLayout and RecyclerView are matched with AppBarLayout to pull down and refresh

Description of the problem:

I want to use SwipeRefreshLayout and RecyclerView to do pull-down and pull-up refresh. These two components are placed in CoordinatorLayout. There is also an AppBarLayout header in CoordinatorLayout. When it is first opened, the AppBarLayout header displays a height of 180dip, from bottom to top Pulling is no problem, it will reduce the AppBarLayout to the height specified by CollapsingToolbarLayout. It is no problem to pull up and down. The problem is that it is pulled down from the top to get the latest data. At this time, the reduced AppBarLayout is not pulled down again. , instead, let SwipeRefreshLayout be loaded at this time, and it will directly become a pull-down refresh. It should be to pull out the hidden part of AppBarLayout and then respond to the pull-down refresh of SwipeRefreshLayout. The following is the xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#cccccc"
    android:layout_height="match_parent" android:fitsSystemWindows="false">

    <android.support.design.widget.AppBarLayout android:id="@+id/app_bar"
        android:fitsSystemWindows="true" android:layout_height="180dip"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout"
            android:fitsSystemWindows="true" android:layout_width="match_parent"
            app:expandedTitleMarginEnd="0dp" app:expandedTitleMarginStart="48dp"
            android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary">

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

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

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cacheColorHint="@null"
            android:scrollbars="vertical" />

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

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


Problem solving:
Inherit the interface implements AppBarLayout.OnOffsetChangedListener in the activity, implement a method onOffsetChanged, and add this method to the AppBarLayout listener. The code is as follows:
public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {

    ...

    @Override
    public void onOffsetChanged (AppBarLayout appBarLayout, int i) {
        swipeRefreshLayout.setEnabled(i == 0);
    }

    @Override
         protected void onResume() {
        super.onResume();
        app_bar.addOnOffsetChangedListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        app_bar.removeOnOffsetChangedListener(this);
    }

    ...

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326957524&siteId=291194637