Java 折叠工具栏布局时隐藏文本视图,展开时显示

Java 折叠工具栏布局时隐藏文本视图,展开时显示,java,android,android-studio,android-layout,Java,Android,Android Studio,Android Layout,我正在使用一个折叠工具栏布局开发一个应用程序,里面有一个ImageView。我想在它上面添加一个渐变,让它看起来更漂亮,并且能够更好地阅读折叠工具栏的标题,所以我做了一点修改,添加了一个相对布局,里面有一个文本视图,然后我在同一个文本视图中添加了一个背景(这就是我所说的渐变)。问题是,当工具栏折叠时,渐变仍然显示在其上,我不希望发生这种情况,当工具栏折叠时,如何使其不可见 设计: <?xml version="1.0" encoding="utf-8"

我正在使用一个折叠工具栏布局开发一个应用程序,里面有一个ImageView。我想在它上面添加一个渐变,让它看起来更漂亮,并且能够更好地阅读折叠工具栏的标题,所以我做了一点修改,添加了一个相对布局,里面有一个文本视图,然后我在同一个文本视图中添加了一个背景(这就是我所说的渐变)。问题是,当工具栏折叠时,渐变仍然显示在其上,我不希望发生这种情况,当工具栏折叠时,如何使其不可见

设计:

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

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

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
            app:collapsedTitleTextAppearance="@style/collapsedToolbarLayoutTitleColor"
            app:expandedTitleTextAppearance="@style/expandedToolbarLayoutTitleColor"
            android:theme="@style/Theme.AnimeWatcher"
            android:id="@+id/anime_page_collapsing_toolbar">

            <ImageView
                android:id="@+id/anime_page_cover"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />
            
            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" >

                <ImageView
                    android:id="@+id/anime_page_back"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_baseline_arrow_back_24"
                    android:paddingEnd="10dp" />

            </androidx.appcompat.widget.Toolbar>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:layout_alignParentBottom="true"
                    android:background="@drawable/black_gradient" />

            </RelativeLayout>


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

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/anime_page_rcv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">


    </androidx.recyclerview.widget.RecyclerView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>


OnOffsetChangedListener
添加到
应用程序栏
中,并在其折叠或展开时收听更改,并在此基础上隐藏/显示您的
文本视图

将ID添加到appbar

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="450dp"
    android:fitsSystemWindows="true">
您可以选择仅使用
alpha
属性而不是
visibility
,这由您自己决定

appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        float percentage = (float) Math.abs(verticalOffset) / appBarLayout.getTotalScrollRange();
        if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
            //  Collapsed
            //Hide your TextView here
            tv.setVisibility(View.GONE);
        } else if(verticalOffset == 0) {
            //Expanded
            //Show your TextView here
            tv.setVisibility(View.VISIBLE);
        } else {
            //In Between
            tv.setVisibility(View.VISIBLE);
            tv.animate().alpha(percentage);
    }
});