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

24

我开始使用新的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 中,因为片段中有更多的元素。

任何帮助将不胜感激。


1
这个问题现在已经在Support Library v23.1.1中得到修复,无需任何变通方法。 - Richard Le Mesurier
@RichardLeMesurier 我不认为完全解决了。我正在使用23.3版本遇到同样的问题。 - stdout
2个回答

10

或许已经有点晚了,但这可能会对新手有所帮助。我想出了一个简单的解决方法。通过创建继承SwipeRefreshLayout的自定义class,您可以在任何存在AppBarLayout的地方使用它。请按照下面的代码进行操作。

    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);
        }
    }

干净利落。谢谢。 - Reaz Murshed
@k0sh,请也回答一下这个问题:http://stackoverflow.com/questions/36434071/android-support-v4-widget-swiperefreshlayout-working-but-it-is-not-visible - user4909407

9

更新:此问题已在Support Library v23.1.1中得到解决。

似乎您需要在AppBarLayout.OnOffsetChangedListener中启用/禁用SwipeRefreshLayout,当AppBarLayout偏移更改时。

请参见https://dev59.com/m10a5IYBdhLWcg3wD1Hb#30822119


Android Support v4小部件SwipeRefreshLayout工作但不可见 - user4909407
1
它回来了,版本号是23.2.1。 - Kosh
此外,针对嵌套RecyclerView的情况,请查看此处https://dev59.com/pJTfa4cB1Zd3GeqPWNXE#36895607 - user1221256

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接