【问题标题】:How to keep the floatingActionButton fixed at is original position when scrolling the recyclerview滚动recyclerview时如何将floatingActionButton固定在原始位置
【发布时间】: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_width="match_parent"
android:layout_height="match_parent"
android:background="@color/accent"> <android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/accent"
>
<android.support.v7.widget.RecyclerView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:dividerHeight="1dp"

/>
        </android.support.constraint.ConstraintLayout>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/addmorefab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    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 留在约束布局?

标签: java android xml android-recyclerview


【解决方案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_width="match_parent"
android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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_width="wrap_content"
    android:layout_height="wrap_content"
    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 时停止工厂的滚动
猜你喜欢
  • 2021-03-17
  • 2019-10-03
  • 2019-04-25
  • 2012-12-14
  • 2016-01-17
  • 2013-04-01
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多