【问题标题】:How to add pull to refresh in Coordinator layout如何在协调器布局中添加拉动刷新
【发布时间】:2019-11-06 08:37:00
【问题描述】:

我在CoordiantorLayout 中有一个AppBarRecyclerView。 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>

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 这个答案与接受的答案相结合为我解决了这个问题。
    【解决方案2】:

    尝试像这样将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>
    

    【讨论】:

    • 当我向下滚动 swiptetorefresh 触发器时,如果我将 swipetorefresh 添加到顶层
    【解决方案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);
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 2021-10-31
      • 1970-01-01
      • 2020-10-03
      • 2018-06-29
      • 1970-01-01
      • 2016-06-14
      相关资源
      最近更新 更多