我尝试在我的Android应用程序中使用以下设置实现正确的滚动行为。
对于导航,我使用jetpack导航结合Toolbar
布局和底部导航。我也使用'一个活动,多个片段'的原则。底部导航的每个项目都会根据我的导航图启动相应的Fragment
。这要求我在布局中使用NavHostFragment
。
我的Toolbar
布局是活动布局的一部分,并根据当前片段进行填充。有些碎片需要折叠的Toolbar
,它也会在需要时添加。但是当有一个折叠工具栏时,我面临以下问题:
在特殊情况下,我有一个崩溃的Toolbar
,NavHostFragment
充满了RecyclerView
。在其他情况下,似乎我可以将app:layout_behavior="@string/appbar_scrolling_view_behavior"
添加到RecyclerView
并获得预期的结果,因为RecyclerView
是CoordinatorLayout
的直接孩子。在我的情况下,RecyclerView
是Fragment
的孩子,基本上是FrameLayout
(根据布局检查员)。这导致了问题,RecyclerView
上的layout_behaviour没有效果,因为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"
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"/>
布局/ toolbar.xml
<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>
布局/ list.xml
<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包装host_fragment,如下所示:
<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>