向上滚动android时自动隐藏或显示工具栏

时间:2019-10-10 08:34:49

标签: android android-toolbar android-coordinatorlayout android-collapsingtoolbarlayout android-appbarlayout

我想在向上滚动以隐藏并向下滚动以显示时隐藏工具栏。使用xml

  

app:layout_scrollFlags =“ scroll | enterAlways | snap”

在工具栏中的

。 请帮我解决我实际需要的输出。滚动到顶部时,工具栏在向下滚动时必须隐藏并显示。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="275dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/ivTop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/apple_1"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:contentInsetStart="0dp"
                android:contentInsetLeft="0dp"
                android:contentInsetEnd="0dp"
                android:contentInsetRight="0dp"
                android:padding="5dp"
                app:contentInsetEnd="0dp"
                app:contentInsetLeft="0dp"
                app:contentInsetRight="0dp"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="parallax"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                app:popupTheme="@style/AppTheme.PopupOverlay">

                <ImageView
                    android:id="@+id/ivBack"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:src="@drawable/icon_back_custom"
                    android:tint="@color/colorPrimary" />
            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:background="@android:color/transparent"
            app:tabBackground="@android:color/transparent"
            app:tabIndicatorColor="@android:color/black"
            app:tabIndicatorHeight="2.50dp"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:tabMode="fixed"
            app:tabPadding="0dp"
            app:tabSelectedTextColor="@android:color/black"
            app:tabTextColor="@android:color/holo_blue_dark" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.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="match_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:id="@+id/tvTitleTab"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start|center_vertical"
                android:padding="10dp"
                android:text="TabSelection"
                android:textAlignment="viewStart"
                android:textColor="@color/colorAccent"
                android:textSize="20sp" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

sample

我该如何实现?我想在向上滚动以隐藏并向下滚动以显示时隐藏工具栏。请帮我解决我实际需要的输出。滚动到顶部时,工具栏在向下滚动时必须隐藏并显示。

1 个答案:

答案 0 :(得分:1)

我找到了解决方案: 只需使用app:layout_scrollFlags="scroll|enterAlways"使您的工具栏和imageView在布局中 像这样

           <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways">
                //ImageView
                //Toolbar
           </RelativeLayout>

完整代码:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="275dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways">
                <ImageView
                    android:id="@+id/ivTop"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/ic_search"
                    android:fitsSystemWindows="true"
                    app:layout_collapseMode="parallax" />

                <<android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:contentInsetStart="0dp"
                    android:contentInsetLeft="0dp"
                    android:contentInsetEnd="0dp"
                    android:contentInsetRight="0dp"
                    android:background="@color/blue_cerulean_"
                    android:padding="5dp"
                    app:popupTheme="@style/AppTheme.PopupOverlay">

                    <ImageView
                        android:id="@+id/ivBack"
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:src="@drawable/ic_search"
                        android:tint="@color/colorPrimary" />
                </android.support.v7.widget.Toolbar>
            </RelativeLayout>


        </android.support.design.widget.CollapsingToolbarLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:background="@android:color/transparent"
            app:tabBackground="@android:color/transparent"
            app:tabIndicatorColor="@android:color/black"
            app:tabIndicatorHeight="2.50dp"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:tabMode="fixed"
            app:tabPadding="0dp"
            app:tabSelectedTextColor="@android:color/black"
            app:tabTextColor="@android:color/holo_blue_dark" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.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="match_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:id="@+id/tvTitleTab"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start|center_vertical"
                android:padding="10dp"
                android:text="TabSelection"
                android:textAlignment="viewStart"
                android:textColor="@color/colorAccent"
                android:textSize="20sp" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>