【问题标题】:Android - Hide TextView with animation after CollapsingToolbarLayout collapsedAndroid - 在 CollapsingToolbarLayout 折叠后隐藏带有动画的 TextView
【发布时间】:2021-05-20 17:18:01
【问题描述】:

CollapsingToolbarLayout被折叠后如何用动画隐藏TextView

TextView branchAddress、branchPhone 和图标搜索。

这是我的 xml 代码:

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

    <NestedScrollView />
</CoordinatorLayout>

See full code here

请帮助我。谢谢。

【问题讨论】:

标签: android android-layout android-coordinatorlayout android-collapsingtoolbarlayout


【解决方案1】:

您可以使用 addOnOffsetChangedListener 来检查您的 appbar 是否折叠并更改 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
        }
    }
})

【讨论】:

  • 感谢您的回答,但找不到班级AppBarStateChangeListener
  • 这里是this post吗?
  • 我希望在滚动 AppBar 时隐藏,显示 TextView,它具有缩放、alpha 等效果。如何存档?
【解决方案2】:

您需要解决几个问题:

  • app:contentScrim 属性中定义透明颜色:这应该是不透明颜色,因为它使用app:layout_collapseMode="parallax" 行为包装ImageView 和文本。我为此使用了任意颜色。

  • 在您要隐藏的几个 textView 的 LinearLayout 中缺少 app:layout_collapseMode="parallax"

您还需要将两个 textView 的 LinearLayout 移动到 MaterialToolbar 下方,因为它们具有不同的折叠行为。

应用这些更改:

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

【讨论】:

  • 感谢您的支持。如果我为app:contentScrim 使用颜色,当 AppBar 折叠时,我希望图像仍然显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 2019-05-31
  • 1970-01-01
  • 2016-03-06
相关资源
最近更新 更多