【问题标题】:Toolbar is below the RecyclerView工具栏位于 RecyclerView 下方
【发布时间】:2021-04-20 15:39:16
【问题描述】:

我有CoordinatorLayout,里面有应用栏布局,里面是折叠工具栏机制。 下面我把进度条和约束布局,recycler view都放在里面了。

问题是:在内容下方滚动工具栏时,我希望它(工具栏)始终向上。 如果有人对布局代码有建议,我很乐意

<?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".fragments.HotelFragment">
        
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="200dp">
    
            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways">
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    android:src="@drawable/landscape3"
                    app:layout_collapseMode="parallax" />
    
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:layout_scrollFlags="scroll|enterAlways" />
            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>
    
    
        <ProgressBar
            android:id="@+id/progress_circle"
            style="@style/Widget.AppCompat.ProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:indeterminateTint="@color/metalic_yellow" />
    
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/cardRecycler"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentStart="true"
                android:layout_alignParentBottom="true"
                android:layout_marginTop="?attr/actionBarSize"
                android:layout_marginBottom="0dp"
                tools:layout_editor_absoluteX="177dp"
                tools:layout_editor_absoluteY="260dp" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.HotelFragment">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="200dp">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:background="@drawable/landscape3"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

             

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_scrollFlags="scroll|enterAlways"
                    app:layout_collapseMode="pin"/>

            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>

    <ProgressBar
        android:id="@+id/progress_circle"
        style="@style/Widget.AppCompat.ProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:indeterminateTint="@color/metalic_yellow" />


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/cardRecycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

看起来很奇怪,实际上什么都没有改变。 也许我使用导航组件库很重要。 下面我放置了负责我的布局和导航机制之间集成的代码。它来自 Google 官方文档。

val layout = binding.collapsingToolbar
val toolbar = binding.toolbar
val navController = findNavController()
val appBarConfiguration = AppBarConfiguration(navController.graph)
layout.setupWithNavController(toolbar, navController, appBarConfiguration)

【问题讨论】:

    标签: android android-layout android-recyclerview toolbar android-coordinatorlayout


    【解决方案1】:

    您可以通过将CoordinatorLayout 包装在另一个布局中来做到这一点,这里我选择了LinearLayout,您可以使用另一个。

    然后在新的根布局中添加工具栏。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".fragments.HotelFragment">
    
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" />
    
        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="200dp">
    
                <com.google.android.material.appbar.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    app:contentScrim="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll|enterAlways">
    
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:fitsSystemWindows="true"
                        android:scaleType="centerCrop"
                        android:src="@drawable/landscape3"
                        app:layout_collapseMode="parallax" />
                </com.google.android.material.appbar.CollapsingToolbarLayout>
    
    
            </com.google.android.material.appbar.AppBarLayout>
    
            <ProgressBar
                android:id="@+id/progress_circle"
                style="@style/Widget.AppCompat.ProgressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:indeterminateTint="@color/metalic_yellow" />
        
        
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
        
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/cardRecycler"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentBottom="true"
                    android:layout_marginTop="?attr/actionBarSize"
                    android:layout_marginBottom="0dp"
                    tools:layout_editor_absoluteX="177dp"
                    tools:layout_editor_absoluteY="260dp" />
            </androidx.constraintlayout.widget.ConstraintLayout>
    
    </LinearLayout>
    

    预览:(工具栏为白色背景)

    更新:

    有没有办法先隐藏工具栏,在折叠工具栏结束后显示?

    为此,您可以:

    • CollapsingToolbarLayout 使用 scroll|exitUntilCollapsed 标志而不是 scroll|enterAlways
    • snapflag 添加到Toolbar,使其成为app:layout_scrollFlags="scroll|enterAlways|snap"
    <?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"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".fragments.HotelFragment">
            
            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="200dp">
        
                <com.google.android.material.appbar.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    app:contentScrim="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">
        
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:fitsSystemWindows="true"
                        android:scaleType="centerCrop"
                        android:src="@drawable/landscape3"
                        app:layout_collapseMode="parallax" />
        
                    <androidx.appcompat.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        app:layout_collapseMode="pin"
                        app:layout_scrollFlags="scroll|enterAlways|snap" />
                </com.google.android.material.appbar.CollapsingToolbarLayout>
            </com.google.android.material.appbar.AppBarLayout>
        
        
            <ProgressBar
                android:id="@+id/progress_circle"
                style="@style/Widget.AppCompat.ProgressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:indeterminateTint="@color/metalic_yellow" />
        
        
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
        
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/cardRecycler"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentBottom="true"
                    android:layout_marginTop="?attr/actionBarSize"
                    android:layout_marginBottom="0dp"
                    tools:layout_editor_absoluteX="177dp"
                    tools:layout_editor_absoluteY="260dp" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    预览

    【讨论】:

    • 感谢您的承诺。有没有办法在折叠工具栏到达它的尽头后隐藏工具栏并显示它?
    • 欢迎......所以你希望它默认隐藏并在 CoordintorLayout 折叠时显示它?
    • 没错。我也试过android:layout_below="@id/appbar",但没有用。
    • @amtrax 请检查答案中的更新部分
    • 非常感谢。出色的工作:))它对我帮助很大。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多