如何在Coordinator布局中添加拉动刷新

问题描述 投票:0回答:5

我在

AppBar
中有一个
RecyclerView
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>

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

android android-recyclerview android-coordinatorlayout android-appbarlayout swiperefreshlayout
5个回答
19
投票

您可以在 AppBar 未完全展开时禁用滑动刷新布局,并在 AppBar 完全展开时启用它。 vAppBar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { _, verticalOffset -> vSwipeRefresh.isEnabled = verticalOffset == 0 })



11
投票

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



9
投票
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>



0
投票

binding.appbar.addOnOffsetChangedListener { _, verticalOffset -> binding.swipeRefreshLayout.isEnabled = verticalOffset == 0 }



-1
投票

更新布局 XML:

打开要添加组件的 XML 布局文件。 包括

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

元素作为父布局。 在

SwipeRefreshLayout
内部,包含
CoordinatorLayout
和其他视图。 这是一个例子:
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        app:popupTheme="@style/ThemeOverlay.MaterialComponents.Light"
        app:title="My Toolbar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_anchor="@id/toolbar"
        app:layout_anchorGravity="bottom" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_scroll_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_arrow_upward"
        app:layout_anchor="@id/recycler_view"
        app:layout_anchorGravity="bottom|end"
        app:layout_margin="@dimen/fab_margin" />

</CoordinatorLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

© www.soinside.com 2019 - 2024. All rights reserved.