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

时间:2019-11-06 08:37:00

标签: android android-recyclerview android-coordinatorlayout android-appbarlayout swiperefreshlayout

我在AppBar中有一个RecyclerViewCoordiantorLayout。 SwipeToRefresh必须为全屏显示,但是RecyclerView不能向下滚动。

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="128dp"/>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>

oid

如何添加全屏拉动以在没有库的情况下刷新Coordinator布局。

3 个答案:

答案 0 :(得分:3)

尝试像这样将SwipeRefreshLayout设置为根父级:

<?xml version="1.0" encoding="utf-8"?>

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />


        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="128dp" />

        </com.google.android.material.appbar.AppBarLayout>


    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

答案 1 :(得分:3)

我像上面的答案一样添加了swipetorefresh顶层。我用下面的代码修复了向上滚动的问题。感谢mohammadReza:)

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            int topRowVerticalPosition =
                    (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
            swipeRefreshLayout.setEnabled(topRowVerticalPosition >= 0);

        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }
    });

答案 2 :(得分:3)

当AppBar未完全展开时,您可以禁用滑动刷新布局,如果 完全展开,则可以启用它。

vAppBar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { _, verticalOffset ->
   vSwipeRefresh.isEnabled = verticalOffset == 0
})