【发布时间】:2021-04-23 07:46:55
【问题描述】:
我使用CollapsingToolbarLayout 和BottomAppBar 来响应CoordinatorLayout 中的滚动变化(折叠并隐藏在滚动上)。因为我不能使用NestedScrollview 作为RecyclerView 的父级,因为当我需要使用scrollToPosition() 或将项目拖出边界时会导致问题(它不会滚动移动项目),滚动行为不适用于CollapsingToolbarLayout 和BottomAppBar。
我尝试了android:nestedScrollingEnabled="true",但没有成功
如何在没有NestedScrollview 的情况下保留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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/Theme.Todolist.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.MaterialComponents.ActionBar"
app:contentScrim="@color/colorPrimary"
app:layout_scrollFlags="exitUntilCollapsed|scroll"
app:toolbarId="@+id/toolbar">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:layout_collapseMode="pin"
app:popupTheme="@style/Theme.Todolist.PopupOverlay"
app:title="Mes tâches"
app:titleTextColor="@color/iconTint"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/tasks_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:clipToPadding="false"
android:paddingBottom="32dp"/>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:hideOnScroll="true"
app:menu="@menu/bottom_app_bar"
app:navigationIcon="@drawable/ic_menu_black_24dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/add_task_text"
app:backgroundTint="@color/floatingAddButton"
app:layout_anchor="@+id/bottomAppBar"
app:layout_anchorGravity="top|center"
app:maxImageSize="50dp"
app:srcCompat="@drawable/ic_add_black_48dp"
app:tint="@color/addIconTint" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
【问题讨论】:
-
在recyclerView中试过属性android:nestedScrollingEnabled="true"??
-
是的,我试过了,但 CoordinatorLayout 功能仍然无法正常工作,collapsesToolbar 没有折叠,bottomNavBar 没有隐藏
-
我发现了错误:在我的 Java 代码中,我将 nestedScrollingEnabled 设置为 false,但我忘记了我做了...现在一切正常
-
那么,您能否将我的答案标记为正确答案????
-
哦,是的,抱歉,已经完成了
标签: android android-recyclerview android-coordinatorlayout android-nestedscrollview