通过RecyclerView、android jetpack导航和折叠工具栏实现正确的滚动

通过RecyclerView、android jetpack导航和折叠工具栏实现正确的滚动,android,android-recyclerview,android-collapsingtoolbarlayout,androidx,android-jetpack-navigation,Android,Android Recyclerview,Android Collapsingtoolbarlayout,Androidx,Android Jetpack Navigation,我尝试在android应用程序中通过以下设置实现正确的滚动行为 对于导航,我将jetpack导航与工具栏布局和底部导航结合使用。我还使用“一个活动,多个片段”的原则。底部导航的每个项目根据我的导航图启动相应的片段。这要求我在布局中使用NavHostFragment 我的工具栏布局是活动布局的一部分,根据当前片段填充。有些片段需要一个折叠的工具栏,需要时也可以添加该工具栏。但是,当有一个折叠工具栏时,我会面临以下问题: 在特定情况下,我有一个正在折叠的工具栏,NavHostFragment将填充一

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

对于导航,我将jetpack导航与
工具栏
布局和底部导航结合使用。我还使用“一个活动,多个片段”的原则。底部导航的每个项目根据我的导航图启动相应的
片段。这要求我在布局中使用
NavHostFragment

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

在特定情况下,我有一个正在折叠的
工具栏
NavHostFragment
将填充一个
RecyclerView
。在其他情况下,我似乎可以将
app:layou behavior=“@string/appbar\u scrolling\u view\u behavior”
添加到
RecyclerView
并获得预期结果,因为
RecyclerView
CoordinatorLayout
的直接子对象。在我的例子中,
RecyclerView
是一个
片段
的子元素,它基本上是一个
FrameLayout
(根据布局检查器)。这导致了一个问题,
RecyclerView
上的布局行为没有任何影响,因为
RecyclerView
不是
协调布局的直接子对象

我想不出解决这个问题的有效办法。有人有主意吗

layout/activity\u 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"
              app:defaultNavHost="true"
              android:layout_marginBottom="?attr/actionBarSize"
              android:layout_marginTop="?attr/actionBarSize"
              app:navGraph="@navigation/nav_graph"
              app:layout_constraintVertical_bias="1.0"/>

    <include layout="@layout/toolbar"
             android:id="@+id/toolbar"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>


<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/overview_bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foregroundGravity="bottom"
        app:menu="@menu/navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:itemBackground="@color/white"
        app:itemIconTint="@color/bottom_navigation_color_states"
        app:itemTextColor="@color/bottom_navigation_color_states"/>
<com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap|snapMargins"
        android:minHeight="?attr/actionBarSize">

    <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
                                                       android:layout_height="wrap_content"
                                                       android:id="@+id/expandedToolbarContentContainer"
                                                       android:layout_marginTop="?attr/actionBarSize"
                                                       app:layout_collapseMode="parallax"/>

    <androidx.appcompat.widget.Toolbar android:layout_width="match_parent"
                                       android:layout_height="?attr/actionBarSize"
                                       app:layout_collapseMode="pin"
                                       style="@style/AppTheme.DarkToolbar"
                                       android:id="@+id/toolbarView">

        <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
                                                           android:layout_height="match_parent"
                                                           android:background="@drawable/round_outline"
                                                           style="@style/AppTheme.DarkToolbar.Container"
                                                           android:id="@+id/toolbarContentContainer"/>

    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.CollapsingToolbarLayout>
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/dish_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layoutAnimation="@anim/layout_animation_fall_down"
    />

布局/toolbar.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"
              app:defaultNavHost="true"
              android:layout_marginBottom="?attr/actionBarSize"
              android:layout_marginTop="?attr/actionBarSize"
              app:navGraph="@navigation/nav_graph"
              app:layout_constraintVertical_bias="1.0"/>

    <include layout="@layout/toolbar"
             android:id="@+id/toolbar"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>


<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/overview_bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foregroundGravity="bottom"
        app:menu="@menu/navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:itemBackground="@color/white"
        app:itemIconTint="@color/bottom_navigation_color_states"
        app:itemTextColor="@color/bottom_navigation_color_states"/>
<com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap|snapMargins"
        android:minHeight="?attr/actionBarSize">

    <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
                                                       android:layout_height="wrap_content"
                                                       android:id="@+id/expandedToolbarContentContainer"
                                                       android:layout_marginTop="?attr/actionBarSize"
                                                       app:layout_collapseMode="parallax"/>

    <androidx.appcompat.widget.Toolbar android:layout_width="match_parent"
                                       android:layout_height="?attr/actionBarSize"
                                       app:layout_collapseMode="pin"
                                       style="@style/AppTheme.DarkToolbar"
                                       android:id="@+id/toolbarView">

        <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
                                                           android:layout_height="match_parent"
                                                           android:background="@drawable/round_outline"
                                                           style="@style/AppTheme.DarkToolbar.Container"
                                                           android:id="@+id/toolbarContentContainer"/>

    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.CollapsingToolbarLayout>
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/dish_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layoutAnimation="@anim/layout_animation_fall_down"
    />


layout/list.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"
              app:defaultNavHost="true"
              android:layout_marginBottom="?attr/actionBarSize"
              android:layout_marginTop="?attr/actionBarSize"
              app:navGraph="@navigation/nav_graph"
              app:layout_constraintVertical_bias="1.0"/>

    <include layout="@layout/toolbar"
             android:id="@+id/toolbar"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>


<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/overview_bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foregroundGravity="bottom"
        app:menu="@menu/navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:itemBackground="@color/white"
        app:itemIconTint="@color/bottom_navigation_color_states"
        app:itemTextColor="@color/bottom_navigation_color_states"/>
<com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap|snapMargins"
        android:minHeight="?attr/actionBarSize">

    <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
                                                       android:layout_height="wrap_content"
                                                       android:id="@+id/expandedToolbarContentContainer"
                                                       android:layout_marginTop="?attr/actionBarSize"
                                                       app:layout_collapseMode="parallax"/>

    <androidx.appcompat.widget.Toolbar android:layout_width="match_parent"
                                       android:layout_height="?attr/actionBarSize"
                                       app:layout_collapseMode="pin"
                                       style="@style/AppTheme.DarkToolbar"
                                       android:id="@+id/toolbarView">

        <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
                                                           android:layout_height="match_parent"
                                                           android:background="@drawable/round_outline"
                                                           style="@style/AppTheme.DarkToolbar.Container"
                                                           android:id="@+id/toolbarContentContainer"/>

    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.CollapsingToolbarLayout>
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/dish_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layoutAnimation="@anim/layout_animation_fall_down"
    />


尝试使用NestedScrollView将主机\u片段包装成所需的行为,如下所示:

 <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:fitsSystemWindows="true"
        android:transitionGroup="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/my_nav_host" />
    </androidx.core.widget.NestedScrollView>

将阻止
滚动定位
回收视图方法
工作。另外,使用
RecyclerView
导航回片段会将其滚动位置重置为顶部

描述了一种更好的方法。甚至在参考文章中也不需要周围的
FrameLayout
s。不要在
NestedScrollView
中包装片段,只需将任何非滚动片段布局视图包装到
NestedScrollView
中即可

带有
NavHostFragment
的活动布局:
无需将片段包装到滚动视图中。只需设置布局行为

。。。
...
根目录下的片段布局
RecyclerView
由于
RecyclerView
具有滚动功能,因此没有什么特别的事情要做


片段布局,例如
TextView
在root
NestedScrollView中换行以添加滚动功能


这将阻止
scrollToPosition
方法的
RecyclerView
工作。另外,使用
RecyclerView
导航回片段会将其滚动位置重置为顶部。请参阅以获得更好的方法。