Search code examples
androidkotlinandroid-recyclerviewfragmentbottomnavigationview

BottomNavigationView and AppBar covers fragment with recyclerview


I have an issue with placing fragment and making it scroll without being overlapped by bottomNavigationView and AppBar. So my content is covered by BottomNavigationView and AppBar.How i can make my content visible when I am scrolling page or appBar and BottomNavigationView invisible while scrolling page down?

my 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>

Is there any solutions?


Solution

  • Make the height of your fragment tag that hold nav host fragment 0dp

    In your 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" />