小编Goo*_*oTZ的帖子

使用 RecyclerView、android jetpack 导航和折叠工具栏实现正确滚动

我尝试在我的 android 应用程序中使用以下设置来实现正确的滚动行为。

对于导航,我将喷气背包导航与Toolbar布局和底部导航结合使用。我也使用“一个活动,多个片段”的原则。底部导航的每个项目都会Fragment根据我的导航图启动一个对应的项目。这要求我NavHostFragment在布局中使用 a 。

我的Toolbar布局是活动布局的一部分,并根据当前片段进行填充。有些片段需要折叠Toolbar,在需要时也会添加。但是当有一个折叠工具栏时,我面临以下问题:

在特定情况下,我有一个折叠的ToolbarNavHostFragment填充有RecyclerView. 在其他情况下,我似乎可以添加app:layout_behavior="@string/appbar_scrolling_view_behavior"RecyclerView并获得预期结果,因为RecyclerViewCoordinatorLayout. 就我而言, theRecyclerView是 a 的子代Fragment,它基本上是 a FrameLayout(根据布局检查器)。这导致了一个问题,即 上的 layout_behaviourRecyclerView没有效果,因为RecyclerView不是 的直接子代CoordinatorLayout

我无法为这个问题想出一个可行的解决方案。有人有想法吗?

布局/activity_overview.xml

<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OverviewActivity">

<androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="wrap_content"
                                                     android:layout_height="wrap_content"
                                                     android:id="@+id/toolbarCoordiantor"
                                                     android:layout_marginTop="?attr/actionBarSize"
                                                     app:layout_constraintTop_toTopOf="parent"
                                                     app:layout_constraintBottom_toTopOf="@id/overview_bottom_navigation"
                                                     app:layout_constraintStart_toStartOf="parent"
                                                     app:layout_constraintEnd_toEndOf="parent">

    <fragment android:layout_width="match_parent" android:layout_height="wrap_content"
              android:id="@+id/overview_fragmentholder"
              android:name="androidx.navigation.fragment.NavHostFragment"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/toolbar"
              app:layout_constraintBottom_toBottomOf="parent" …
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview android-collapsingtoolbarlayout androidx android-jetpack-navigation

5
推荐指数
1
解决办法
823
查看次数