【问题标题】:Bottom sheet fragments not showing when a FragmentContainerView is part of the layout当 FragmentContainerView 是布局的一部分时,底部表单片段未显示
【发布时间】:2021-02-23 10:40:44
【问题描述】:

由于底部的工作表片段没有显示出来,我最初认为问题出在我的 Java 代码中——可能是适配器中的设置不正确、使用了错误的片段管理器等——而不是 XML 布局。经过几天的挫折,我终于确定了罪魁祸首。

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

当底部工作表处于折叠状态时,其选项卡及其标签可见(在peekHeight),我可以点击它们。但是,当底部工作表展开时,选项卡仍然可见,但它们的片段中没有显示任何内容。我已经确定,如果我注释掉 FragmentContainerView - 在这里,如果我使用旧的 fragment 视图真的没关系 - 底部工作表按预期工作,所有选项卡中的所有内容都会显示。

那么发生了什么?

【问题讨论】:

  • 你能添加图片吗,它看起来怎么样?

标签: android android-fragments


【解决方案1】:

首先,您的 TabLayout 需要 app:layout_constraintTop_toTopOf="parent"

并使 ViewPager 高度不是 0dp,例如 android:layout_height="match_parent"500dp .. 类似的东西。

如果这不能解决问题,您可以使用 RelativeLayoutLinearLayout 等其他布局作为 BottomSheet 根。

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2012-09-10
    相关资源
    最近更新 更多