使用Recyclerview和SwipeRefreshLayout折叠工具栏无法正常工作

时间:2015-11-12 16:00:07

标签: android swiperefreshlayout android-collapsingtoolbarlayout

我有一个Collpasing工具栏和一个具有多个布局的RecyclerView。当我向上滚动它的罚款。但是当向上滚动时,SwipeToRefresh将触发。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbarLayout"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


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

            <ImageView
                android:id="@+id/img_content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/juandirection_samplepic"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


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

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

    <include layout="@layout/content_category_selected" />
</android.support.design.widget.CoordinatorLayout>  

当用户点击顶部时,SwipeToRefresh应该可以正常工作,当您按住屏幕时,将会触发刷卡以刷新。在我的情况下,每次滑动(向下),刷卡刷新都会触发。我案的任何解决方案?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".CategorySelected"
    tools:showIn="@layout/activity_category_selected">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

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

</RelativeLayout>

1 个答案:

答案 0 :(得分:-1)

试试这个:

mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
             if(verticalOffset == headerhight){
//headerhight is the offset when appbarlayout height when expanded completely.
                 mSwipeLayout.setRefresh(false);
             }else{
                 mSwipeLayout.setRefresh(true);
             }
        }
    });

但这个有一个错误。