首页 > 解决方案 > 在具有相对布局父级的片段中使用持久底部表

问题描述

我有一个以协调器布局为父的活动,该活动由工具栏和 viewpager tablayout 组成。该活动有一个具有相对布局父级的片段,并且嵌套在这个相对布局中的是一个recyclerview和一个底部工作表。

现在的问题是底部工作表不适用于相对布局,但工具栏在 recyclerview 滚动事件上滚动良好。但是,当我使用协调器布局作为片段布局的父级时,底部工作表工作正常,但选项卡无法滚动,因此底部工作表未完全显示。

我的问题:如何在 relativelayout 父级中使用 bootomsheet。或者我仍然可以在片段中使用协调器布局并且父选项卡仍然可以很好地滚动?

以下是将相对布局作为根布局的片段的代码:

<RelativeLayout
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    <include layout="@layout/report_question_bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

这是以协调器布局作为根布局的片段的代码

<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  <include layout="@layout/report_question_bottom_sheet"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

这是我的带有选项卡布局的活动的代码

<androidx.coordinatorlayout.widget.CoordinatorLayout 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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".Bookmarks">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_weight="1"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

        </androidx.appcompat.widget.Toolbar>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"
            app:tabGravity="fill"/>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       app:layout_behavior="@string/appbar_scrolling_view_behavior" />   

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我希望工具栏在向下滚动 recyclerview 项目时滚动出视图,并在向上滚动 recyclerview 项目时滚动到视图中。

标签: androidandroid-recyclerviewandroid-tablayoutandroid-coordinatorlayoutbottom-sheet

解决方案


您可以查看文档

BottomSheetBehavior应用于一个子级CoordinatorLayout以使该子级成为一个持久的底部工作表。

BottomSheetBehavior协同工作CoordinatorLayout,让您在底部工作表上显示内容。


推荐阅读