滚动recyclerview时如何将floatingActionButton固定在原始位置

Posted

技术标签:

【中文标题】滚动recyclerview时如何将floatingActionButton固定在原始位置【英文标题】:How to keep the floatingActionButton fixed at is original position when scrolling the recyclerview 【发布时间】:2021-10-01 12:33:32 【问题描述】:

向上或向下滚动时,我需要帮助将图片中显示的工厂保持在固定位置 这是我的xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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:layout_
android:layout_
android:background="@color/accent"> <android.support.constraint.ConstraintLayout 
    android:layout_
    android:layout_
    android:background="@color/accent"
>
<android.support.v7.widget.RecyclerView
    android:id="@+id/listView"
    android:layout_
    android:layout_
    android:dividerHeight="1dp"

/>
        </android.support.constraint.ConstraintLayout>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/addmorefab"
    android:layout_
    android:layout_
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="@dimen/fab_marginBottom"
    android:layout_marginRight="@dimen/fab_marginRight"
    android:clickable="true"
    app:backgroundTint="@color/primary"
    app:srcCompat="@drawable/ic_action_new"
    app:layout_anchor="@id/listView"
    app:layout_anchorGravity="bottom|right|end"
    /></android.support.constraint.ConstraintLayout>

我什至尝试制作两个约束布局,但它不起作用

我也尝试过使用 recyclerView.setNestedScrollingEnabled(false); 它有效,但它限制了recyclerview的滚动。 在第二张图片上,我希望工厂在不滚动的地方保持固定

【问题讨论】:

为什么不将FAB 留在约束布局? 【参考方案1】:

如果您希望明亮的按钮在滚动回收站时保持固定,您可以使用此视图。

我使用的是 Android X 库,您可以使用较低版本的库。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_
android:layout_>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/listView"
    android:layout_
    android:layout_
    android:dividerHeight="1dp"

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/addmorefab"
    android:layout_
    android:layout_
    android:layout_marginRight="18dp"
    android:layout_marginBottom="18dp"
    android:clickable="true"
    app:backgroundTint="@color/colorPrimary"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:srcCompat="@android:drawable/ic_input_add" />

   </androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

谢谢你的回答我试过了,但它不起作用 给出更完整的解释或发送您的项目,以便我们确切知道您想要什么 好吧,当我滚动 recyclerView 时,fab 和工具栏会向上滚动,直到 appbar 布局内的工具栏消失。我想要的是在滚动 recyclerView 时停止工厂的滚动

以上是关于滚动recyclerview时如何将floatingActionButton固定在原始位置的主要内容,如果未能解决你的问题,请参考以下文章

当另一个滚动时如何隐藏recyclerview

插入数据时Recyclerview自动向上滚动

在滚动时在 recyclerview 中加载更多数据

RecyclerView更新时如何自动向下滚动

滚动 NestedScrollView 时如何获取 recyclerview 的当前项目位置?

滚动时如何扩展 RecyclerView 项之间的空间