【问题标题】:Is there any way to keep my Extended Floating action Button Fixed Position When using scrolling behavior使用滚动行为时,有什么方法可以保持我的扩展浮动操作按钮固定位置
【发布时间】:2021-05-06 12:03:17
【问题描述】:

这是我的主要activity.xml 文件,它是导航页面。选择时,每个导航菜单项都会在不同的协调器布局文件上膨胀。 我使用 Co-ordinator Layout 因为我想在滚动列表时隐藏应用栏。我的活动布局完全按照我的意愿工作,但片段布局是问题所在。

<?xml version="1.0" encoding="utf-8"?>

<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".Home">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/purple_700"
                android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/Theme.MaterialComponents.Light"


                />
        </com.google.android.material.appbar.AppBarLayout>


        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:background="@color/white"
            app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">


        </androidx.coordinatorlayout.widget.CoordinatorLayout>


    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/home_nav"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_menu" />

</androidx.drawerlayout.widget.DrawerLayout>


这是我的片段布局 XML...我使用回收器视图来填充对象列表。问题是当我滚动列表时,扩展 FAB 也在我向上滚动时向上移动,当我向下滚动时向下移动。我希望这个 E FAB 在我滚动列表时保持固定。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    android:background="@color/white">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/assembly_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white" />

    <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/new_assembly"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="81dp"
        android:contentDescription="@string/new_assembly"

        android:text="@string/new_assembly"
        app:icon="@drawable/ic_add"
        app:layout_anchor="@id/assembly_recycler"
        app:layout_anchorGravity="bottom|end" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>

每当我滚动回收站视图时,扩展浮动操作按钮也会滚动。

有什么方法可以让我的扩展 FAB 像 Gmail 应用程序中的每个片段一样保持固定。

【问题讨论】:

  • 您的工厂操作片段是否相关?我的意思是片段 A 有这个 EFab 其他人不会有这个吗?为什么使用协调器布局作为片段容器。为什么不用框架布局?

标签: android android-support-library floating-action-button android-coordinatorlayout vertical-scrolling


【解决方案1】:

我使用CoordinatorLayout 是因为我想隐藏应用栏 滚动列表。我的活动布局完全按照我的意愿工作 但是片段布局是个问题。

主布局中定义的CoordinatorLayout 似乎可以正常工作,但您为容器设置的另一个CoordinatorLayout 似乎是不必要的。尝试将其替换为 FrameLayout 作为您的内容容器。

有什么方法可以让我的扩展 FAB 像在 Gmail 应用程序中一样保持固定 在每个片段上。

您需要在 Activity 布局中设置 Extended FAB 并使用 FrameLayout 而不是 CoordinatorLayout 作为容器:(这是正确的方法)

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/Theme.MaterialComponents.Light" />
        </com.google.android.material.appbar.AppBarLayout>

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
            android:id="@+id/new_assembly"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="16dp"
            app:layout_anchorGravity="bottom|end" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/home_nav"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_view_header"
        app:menu="@menu/navigation_menu" />

</androidx.drawerlayout.widget.DrawerLayout>

对于您的Fragment 布局,您可以使用ConstraintLayout

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/assembly_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 2016-08-01
    • 2016-12-05
    • 1970-01-01
    相关资源
    最近更新 更多