问题描述
这是我的主要 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 应用程序中一样固定在每个片段上。
解决方法
我使用 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>