BottomNavigationView 和 AppBar 用 recyclerview 覆盖片段

BottomNavigationView and AppBar covers fragment with recyclerview

我在放置片段并使其滚动而不被 bottomNavigationView 和 AppBar 重叠时遇到问题。所以我的内容被 BottomNavigationView 覆盖,AppBar.How 我可以在滚动页面时让我的内容可见,或者在向下滚动页面时让 appBar 和 BottomNavigationView 不可见?

我的activity_main.xml:

<?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/drawerLayout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:id="@+id/mainToolbar"
        style="@style/mainToolbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:elevation="0dp"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="8dp"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
        android:background="@drawable/shadow"/>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_menu"/>

    <fragment
        android:layout_marginTop="130dp"
        android:id="@+id/dataContainer"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="409dp"
        android:layout_height="673dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/mainToolbar"
        app:navGraph="@navigation/my_nav" />

</androidx.constraintlayout.widget.ConstraintLayout>

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

</androidx.drawerlayout.widget.DrawerLayout>

有解决办法吗?

将包含导航主机片段的片段标签的高度设置为 0dp

在你的activity_main.xml

<fragment
    android:layout_marginTop="130dp"
    android:id="@+id/dataContainer"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="409dp"
    android:layout_height="673dp"  ---------> ( make it 0dp )
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/mainToolbar"
    app:navGraph="@navigation/my_nav" />