android - 向下滚动触发刷新而不是显示工具栏

标签 android swiperefreshlayout android-coordinatorlayout

我开始使用新的 CoordinatorLayout 时遇到了这个问题:

demo

当我尝试向下滚动时,当工具栏部分可见并且 recylcerview 位于顶部位置时,您可以看到它触发刷新事件而不是下拉工具栏。用户必须向上滚动而不是向下滚动才能显示它。

布局 XML 看起来像这样: activity_main.xml:

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        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">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                style="@style/customActionBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"/>

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

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        app:headerLayout="@layout/navigation_drawer_header"
        app:menu="@menu/navigation_items" />

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

fragment_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:padding="8dp"
            android:scrollbars="none" />

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

</FrameLayout>

SwipeRefreshLayout 位于 FrameLayout 中,因为我的应用程序的 fragment 中有更多元素。

任何帮助将不胜感激。

最佳答案

可能为时已晚,但它可能会帮助任何新来者,我想出了一个简单的解决方案。通过创建扩展 SwipeRefreshLayout 的自定义 class 您可以在 AppBaLayout 存在的任何地方使用它,请遵循以下代码

    public class MySwipeLayout extends SwipeRefreshLayout implements AppBarLayout.OnOffsetChangedListener {
        private AppBarLayout appBarLayout;

        public MySwipeLayout(Context context) {
            super(context);
        }

        public MySwipeLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
            if (getContext() instanceof Activity) {
                appBarLayout = (AppBarLayout) ((Activity) getContext()).findViewById(R.id.appbar);
                appBarLayout.addOnOffsetChangedListener(this);
            }
        }

        @Override
        protected void onDetachedFromWindow() {
          if(appBarLayout!=null){
            appBarLayout.removeOnOffsetChangedListener(this);
            appBarLayout = null;
          }
          super.onDetachedFromWindow();
        }

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

关于android - 向下滚动触发刷新而不是显示工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30833589/

相关文章:

java - Android Java 倒计时然后做某事

android - androidx 迁移后,react-native 应用程序上出现 SwipeRefreshLayout 错误

android - 试图通过向左或向右滑动来删除 RecyclerView 项目(从网络服务加载)

java - 主题错误 - 如何修复?

java - 从 viewpager 适配器设置页面标题后,页面标题不会出现在 ViewPager 中

android - 为 Android 开发时,如何在 AsyncTask 中使用 Eclipse 调试器?

android - unity project as library和android project后manifests出现错误

Android错误膨胀类SwipeRefreshLayout

android - 当键盘打开时,包裹在 ScrollView 中的 ConstraintLayout 仍然会调整大小

android - 如何在协调器布局中添加拉动刷新