首页 > 解决方案 > 滚动 NestedScrollView 的内容后如何显示工具栏

问题描述

当嵌套滚动视图内容向上滚动并在内容向下滚动时隐藏它时,我试图将线性布局显示为我的工具栏。

我正在使用 CoordinatorLayout 和 CollapsingToolbarLayout。当折叠布局获取的内容消失,并且 NestedScrollView 到达主布局的顶部时,应该显示上述布局。此外,一旦 NestedScrollView 从主布局顶部下降并且 CollapsingToolbarLayout 的内容想要显示出来,上述布局就会消失。

这是我的 XML 代码:

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


    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layoutDirection="rtl"
        android:orientation="vertical"

        tools:context=".home.HomeFragment">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/gray_light">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsing"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlwaysCollapsed">

           <!--       I want this layout be visible when the nested scroll view scrolls up
                      and hide it when the nestedScrollView scrolls down -->
                <LinearLayout
                    android:elevation="10dp"
                    app:layout_collapseMode="pin"
                    android:background="@color/blue"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"/>

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_collapseMode="parallax">


                    <androidx.viewpager2.widget.ViewPager2
                        android:id="@+id/slider"
                        android:layout_width="match_parent"
                        android:layout_height="180dp"
                        android:background="@color/white"
                        android:elevation="5dp"
                        android:orientation="horizontal"
                        app:layout_constraintTop_toTopOf="parent" />

                    <com.tbuonomo.viewpagerdotsindicator.WormDotsIndicator
                        android:id="@+id/dots_indicator"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        app:dotsColor="@color/blue"
                        app:dotsCornerRadius="8dp"
                        app:dotsSize="16dp"
                        app:dotsSpacing="4dp"
                        app:dotsStrokeColor="@color/gray"
                        app:dotsWidthFactor="2.5"
                        app:layout_constraintLeft_toLeftOf="parent"
                        app:layout_constraintRight_toRightOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/slider" />


                    <LinearLayout
                        android:id="@+id/best_users_header"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:gravity="center_vertical"
                        app:layout_constraintTop_toBottomOf="@+id/dots_indicator">

                        <ImageView
                            android:layout_width="30dp"
                            android:layout_height="40dp"
                            android:layout_marginStart="5dp"
                            android:src="@mipmap/ic_cup"
                            tools:ignore="ContentDescription" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_marginEnd="10dp"
                            android:text="@string/best_users"
                            android:textAppearance="@style/vazirFont" />

                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="1.5dp"
                            android:layout_gravity="center"
                            android:layout_marginTop="2dp"
                            android:layout_weight="0.5"
                            android:background="@color/blue_soft" />

                        <TextView
                            android:id="@+id/all_best_users"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_marginStart="10dp"
                            android:gravity="center"
                            android:text="@string/all"
                            android:textAppearance="@style/vazirFont"
                            android:textColor="@color/blue_dark"
                            app:drawableEndCompat="@drawable/ic_baseline_chevron_left_24" />


                    </LinearLayout>

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/best_users_recyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:background="@color/gray_soft"
                        android:clipToPadding="false"
                        android:overScrollMode="never"
                        android:paddingStart="10dp"
                        android:paddingTop="10dp"
                        android:paddingEnd="10dp"
                        android:paddingBottom="10dp"
                        app:layout_constraintTop_toBottomOf="@+id/best_users_header" />
                </androidx.constraintlayout.widget.ConstraintLayout>

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

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


        <androidx.core.widget.NestedScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/gray_light"
            android:elevation="10dp"
            android:overScrollMode="never"
            app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/list_of_courses_header"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:gravity="center_vertical"
                    app:layout_constraintTop_toBottomOf="@+id/best_users_recyclerView">

                    <ImageView
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:layout_marginStart="5dp"
                        android:src="@mipmap/ic_books"
                        tools:ignore="ContentDescription" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_marginEnd="10dp"
                        android:text="@string/list_of_courses"
                        android:textAppearance="@style/vazirFont" />

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="1.5dp"
                        android:layout_gravity="center"
                        android:layout_marginTop="2dp"
                        android:layout_weight="0.5"
                        android:background="@color/blue_soft" />


                </LinearLayout>

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/course_recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:overScrollMode="never" />

            </LinearLayout>

        </androidx.core.widget.NestedScrollView>



    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

实际上,我希望注释布局成为 NestedScrollView 内容的标题,并通过向上滚动在布局顶部可见,并在 NestedScrollView 向下滚动并且折叠布局的内容可见时消失。

标签: androidxmlandroid-toolbarandroid-collapsingtoolbarlayoutandroid-nestedscrollview

解决方案


推荐阅读