【问题标题】:CollapsingToolbar in NavDrawer stuck in expanded position on scrollNavDrawer 中的 CollapsingToolbar 在滚动时卡在展开位置
【发布时间】:2020-03-02 23:00:54
【问题描述】:

由于某种原因,当我滚动下方的 RecyclerView 时,我的 CollapsingToolbar(在 NavigationDrawer 内)似乎永远不会移动。我错过了一些简单的事情吗?我怎样才能让它解开,以便文本将自己定位在滚动上的汉堡图标旁边?

抽屉布局

<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">


    <androidx.drawerlayout.widget.DrawerLayout
            android:id="@+id/myDrawerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/cl_md"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            <LinearLayout
                    android:id="@+id/masterToolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toTopOf="@+id/master_container"
                    app:layout_constraintTop_toTopOf="parent">

                <include layout="@layout/layout_collapsingtoolbar" />
            </LinearLayout>

            <RelativeLayout
                    android:id="@+id/master_container"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/masterToolbar" />
        </androidx.constraintlayout.widget.ConstraintLayout>


        <!-- Container for contents of drawer -->
        <com.google.android.material.navigation.NavigationView
                android:id="@+id/my_navview"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:fitsSystemWindows="true"
                app:menu="@menu/menu_navdrawer" />

    </androidx.drawerlayout.widget.DrawerLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

工具栏

<?xml version="1.0" encoding="utf-8"?>
<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">

    <com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/myAppBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/myCollapsingToolbarLayout"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="scroll|snap|exitUntilCollapsed">

            <androidx.appcompat.widget.Toolbar
                    android:id="@+id/myToolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin" />
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

    标签: android xml android-recyclerview android-collapsingtoolbarlayout android-appbarlayout


    【解决方案1】:

    只需添加CoordinatorLayout 即可:)

    要在布局中添加折叠工具栏,请将 AppBarLayout 内的 CollapsingToolbarLayout。然后,将 Toolbar 和任何其他视图作为子项添加到 CollapsingToolbarLayout。

    确保整个视图结构位于 CoordinatorLayout 内,以利用 CollapsingToolbarLayout 的滚动和功能。

    <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">
    
    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleGravity="top"
        app:expandedTitleMarginStart="@dimen/shrine_toolbar_offset_start"
        app:expandedTitleMarginTop="@dimen/shrine_toolbar_offset_top"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
    
      <ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="@dimen/shrine_toolbar_image_offset_top"
          app:layout_collapseMode="parallax"
          app:layout_collapseParallaxMultiplier="0.5"/>
    
      <androidx.appcompat.widget.Toolbar
          android:id="@+id/AppBar"
          android:layout_width="match_parent"
          android:layout_height="@dimen/shrine_toolbar_collapsed_height"
          app:layout_collapseMode="pin"/>
    </com.google.android.material.appbar.CollapsingToolbarLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    Reference

    【讨论】:

    • 你从哪里得到@dimen/shrine_toolbar_offset_start@dimen/shrine_toolbar_offset_top
    • 它们似乎是dimen 文件,但没有查看或下载它们的链接。
    • 请注意RecyclerView 位于折叠工具栏下方,因此这听起来很复杂。
    • 好的,我认为这就是问题所在。我将不得不回到正常的Toolbar。惭愧,因为我讨厌不得不伸出拇指才能到达RecyclerView 中的顶部项目。
    猜你喜欢
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多