Bottom sheet fragments not showing when a FragmentContainerView is part of the layout

  android, android-fragments

With the bottom sheet fragments not showing up, I initially thought the problem was within my Java code — maybe improper setup in the adapter, using the wrong fragment manager, etc — and not in the XML layouts. After a couple days of frustration, I finally identified the culprit.

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

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="?android:attr/actionBarSize"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:defaultNavHost="true"
        app:navGraph="@navigation/analytics"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/color_surface"
        android:elevation="@dimen/size_2"
        app:behavior_peekHeight="?android:attr/actionBarSize"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"/>

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tabs"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

When the bottom sheet is at collapsed, its tabs and their labels are visible (at the peekHeight), and I can tap through them. However, when the bottom sheet is expanded, the tabs are still visible but nothing is showing in their fragments. I’ve identified that if I comment out the FragmentContainerView — and here it really doesn’t matter if I use the old fragment view instead — the bottom sheet works as expected, with all content in all tabs getting displayed.

So what’s going on?

Source: Android Questions

LEAVE A COMMENT