Android-折叠工具栏布局折叠后隐藏带动画的文本视图

Android-折叠工具栏布局折叠后隐藏带动画的文本视图,android,android-layout,android-coordinatorlayout,android-collapsingtoolbarlayout,Android,Android Layout,Android Coordinatorlayout,Android Collapsingtoolbarlayout,如何在折叠工具栏布局折叠后用动画隐藏文本视图 text查看branchAddress、branchPhone和图标搜索 这是我的xml代码: <CoordinatorLayout> <AppBarLayout> <CollapsingToolbarLayout> <ImageView /> <Toolbar /> <LinearLay

如何在
折叠工具栏布局
折叠后用动画隐藏
文本视图

text查看branchAddress、branchPhone和图标搜索

这是我的xml代码:

<CoordinatorLayout>
    <AppBarLayout>
        <CollapsingToolbarLayout>
            <ImageView />
            <Toolbar />
            <LinearLayout />
        </CollapsingToolbarLayout>
    </AppBarLayout>

    <NestedScrollView />
</CoordinatorLayout>


请帮帮我。谢谢。

您可以使用addOnOffsetChangedListener检查应用程序栏是否折叠,并更改textview的可见性

如下图所示:

appbar.addOnOffsetChangedListener(object : AppBarStateChangeListener() {
    override fun onStateChanged(
        appBarLayout: AppBarLayout?,
        state: State?
    ) {
        if(state == State.COLLAPSED){
            text = View.GONE
        }else if(state == State.EXPANDED){
            text.visibility = View.VISIBLE
        }
    }
})

您需要解决以下几个问题:

  • app:contentScrim
    属性中定义透明颜色:这应该是不透明的颜色,因为它使用
    app:layout\u collapseMode=“parallax”
    行为包装
    ImageView
    和文本。我用了一种任意的颜色

  • 要隐藏的两个文本视图的
    线性布局中缺少
    app:layout\u collapseMode=“parallax”

此外,还需要将两个textView的
线性布局
移动到
材质工具栏
的下方,因为两者具有不同的折叠行为

应用这些更改:

<?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"
    android:fitsSystemWindows="true"
    tools:context=".presentation.common.view.Demo2HomeFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:fitsSystemWindows="true"
            app:contentScrim="@android:color/holo_blue_dark"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            app:statusBarScrim="@android:color/transparent">

            <com.google.android.material.imageview.ShapeableImageView
                android:id="@+id/imgHeaderBackground"
                android:layout_width="match_parent"
                android:layout_height="192dp"
                android:contentDescription="@null"
                android:fitsSystemWindows="true"
                android:foreground="@drawable/img_header_background_overlay"
                android:scaleType="centerCrop"
                android:src="@color/colorPrimary"
                app:layout_collapseMode="parallax"
                app:shapeAppearanceOverlay="@style/ImageviewRoundedBottom48dp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                app:layout_collapseMode="parallax"
                android:layout_marginBottom="48dp"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/txtBranchAddress"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="{branch.address}"
                        android:textAppearance="@style/TextAppearance.MaterialComponents.Body1"
                        android:textColor="@color/white"
                        tools:ignore="HardcodedText" />

                    <TextView
                        android:id="@+id/txtBranchPhone"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="4dp"
                        android:text="{branch.phone}"
                        android:textAppearance="@style/TextAppearance.MaterialComponents.Body1"
                        android:textColor="@color/white"
                        tools:ignore="HardcodedText" />
                </LinearLayout>

                <ImageView
                    android:id="@+id/btnSearch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="?attr/actionBarItemBackground"
                    android:contentDescription="@null"
                    android:padding="8dp"
                    android:src="@drawable/ic_search" />
            </LinearLayout>

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:paddingStart="16dp"
                android:paddingTop="8dp"
                android:paddingEnd="16dp"
                android:paddingBottom="8dp"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="pin">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <ImageView
                        android:id="@+id/imgBranchLogo"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:contentDescription="@null"
                        android:scaleType="fitStart"
                        android:src="@mipmap/ic_launcher" />

                    <ImageView
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:contentDescription="@null"
                        android:src="@drawable/abc_vector_test" />
                </LinearLayout>
            </com.google.android.material.appbar.MaterialToolbar>

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

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

        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>


检查。虽然这描述了淡出整个
相对论yout
,但对于您的支持,这个概念似乎是相同的。如果我对app:contentScrim使用颜色,当AppBar折叠时,我希望图像显示为静止。感谢您的回答,但是找不到类
AppBarStateChangeListener
。它在这里吗?我希望当滚动AppBar隐藏时,显示TextViView,它具有缩放、alpha等效果。那怎么办?