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

ksg*_*rkn 9 android swiperefreshlayout android-recyclerview android-coordinatorlayout android-appbarlayout

我有一个AppBarRecyclerViewin CoordiantorLayout。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>
Run Code Online (Sandbox Code Playgroud)

类

如何在没有库的协调器布局中添加全屏下拉刷新。

ksg*_*rkn 9

我像上面的答案一样添加了 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);
        }
    });
Run Code Online (Sandbox Code Playgroud)


ars*_*ent 9

您可以禁用刷卡刷新布局时AppBar没有完全展开,并启用它,如果它完全展开。

vAppBar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { _, verticalOffset ->
   vSwipeRefresh.isEnabled = verticalOffset == 0
}) 
Run Code Online (Sandbox Code Playgroud)


moh*_*iri 5

尝试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>
Run Code Online (Sandbox Code Playgroud)

  • 当我向下滚动 swiptetorefresh 时,如果我将 swipetorefresh 添加到顶层,则会触发 (5认同)