SwipeRefreshLayout和RecyclerView配和AppBarLayout下拉刷新时的问题

问题描述:

想用SwipeRefreshLayout和RecyclerView做下拉跟上拉刷新,这两个组件配合放在CoordinatorLayout中,CoordinatorLayout中还有一个AppBarLayout头,第一次打开的时候AppBarLayout头显示180dip的高度,从下往上拉没问题,会把AppBarLayout缩小到CollapsingToolbarLayout指定的高度,上往上拉是没问题,问题是从上往下拉,这种是拉获取最新的数据,这时候没有把缩小的AppBarLayout给再重新拉下来,反而是让SwipeRefreshLayout载到了这时候直接就成了下拉刷新了,应该是把AppBarLayout隐藏的那部分拉出来之后再响应SwipeRefreshLayout的下拉刷新,下面是xml文件
<?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>


问题解决:
在activity中继承接口 implements AppBarLayout.OnOffsetChangedListener,实现一个方法onOffsetChanged,完了把这个方法添加到AppBarLayout监听中就行了,代码如下:
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);
    }

    ...

}

猜你喜欢

转载自iaiai.iteye.com/blog/2284752