使用 CollapsingToolbar 滚动时如何更新工具栏

How can I update the toolbar when scrolling with CollapsingToolbar

我正在尝试从一个 TextView 移动到 Toolbar 滚动时折叠。

假设我有一个工具栏,在它的正下方有一个视图,当我向上滚动时它开始完全不可见我想用这个文本更新工具栏,这是一个例子。

第一个场景

第二种情况

我一直在使用 CollapsingToolbar 进行阅读,但我可以在视图不那么可见时弄清楚,如果我必须创建一个自定义工具栏,并在右侧设置一个文本,因为不是标题,是标题的额外值。

where I can set, because it's not the title, it's an extra value of the title.

您可以将标题用于自定义 Toolbar 作为 supportActionBar;但将滚动标志适当地调整到工具栏,并将布局行为调整到主布局。

if I have to create like a custom Toolbar with a text at the right

将标题设置到右边;有 2 个属性,一个用于折叠文本,另一个用于展开文本,您需要将它们对齐到 end/right:

app:collapsedTitleGravity="end"
app:expandedTitleGravity="end"

这是一个演示:

<?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=".MainActivity">


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbarlayout"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:paddingEnd="8dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:collapsedTitleGravity="end"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleGravity="end"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="Hi">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                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"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:title="Hi"
                app:titleTextColor="#fff" />

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

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


    <!--    Main Layout -->

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/appbarlayout">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/long_text" />

    </androidx.core.widget.NestedScrollView>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

确保使用 NoActionBar 主题,并将自定义工具栏设置为 supportActionBar:

val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)

更新:

perhaps I missexplained the issue, the thing is, I have a Toolbar and just below, I have like a LinearLayout with Text, and when I scroll and this LinearLayout is going to hide I want to show the text it was in the LinearLayout. So the idea is, Something like you did, but instead of having this "Hi" in the Toolbar, have it in a view just below the Toolbar and when I scroll and it starts to get invisible / non readable just put this "Hi" text in the Toolbar.

您可以使用自定义 TextViewCollapsingToolBar 的标题来执行此操作,并使用 OnOffsetChangedListener

以编程方式切换两者的可见性

首先,为折叠和展开状态创建以下样式:

    <style name="Title.Collapsed" parent="android:TextAppearance">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textSize">18sp</item>
    </style>

    <style name="Title.Expanded" parent="android:TextAppearance">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textSize">28sp</item>
    </style>

这些样式将分别应用于 CollapsingToolbarLayout 上的 app:collapsedTitleTextAppearanceapp:expandedTitleTextAppearance

布局:

<?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
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:collapsedTitleGravity="end"
            app:collapsedTitleTextAppearance="@style/Title.Collapsed"
            app:expandedTitleGravity="end"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleTextAppearance="@style/Title.Expanded"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="40dp"
                android:background="@android:color/transparent"
                android:gravity="end"
                android:orientation="vertical"
                android:padding="10dp"
                app:layout_collapseMode="parallax">

                <TextView
                    android:id="@+id/tv_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Hi"
                    android:textSize="28sp" />
            </LinearLayout>

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/design_default_color_primary"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/appBarlayout">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/long_text" />

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

检测 appBarLayout 滚动变化 addOnOffsetChangedListener:

val collapsingToolbar = findViewById<CollapsingToolbarLayout>(R.id.collapsingToolbar)
collapsingToolbar.title = ""
title = ""

val appBarLayout = findViewById<AppBarLayout>(R.id.appBarLayout)
appBarLayout.addOnOffsetChangedListener(object : OnOffsetChangedListener {
    var isShow = false
    var scrollRange = -1
    override fun onOffsetChanged(appBarLayout: AppBarLayout, verticalOffset: Int) {
        if (scrollRange == -1) {
            scrollRange = appBarLayout.totalScrollRange
        }
        if (scrollRange + verticalOffset == 0) {
            //when collapsingToolbar at that time display actionbar title
            collapsingToolbar.title = "Hi"
            isShow = true
        } else if (isShow) {
            collapsingToolbar.title = ""
            isShow = false
        }
    }
})