在Android上折叠后隐藏工具栏

时间:2016-01-03 17:20:10

标签: android android-support-library android-toolbar

我想拥有像instagram这样的东西,因为它非常酷!他们的卡片视图中的工具栏或其列表视图中的视图。如果其中一个与另一个折叠,则上方 工具栏消失 ,下方的工具栏取代工具栏。

我有MainActivityFragment的这些代码。

我的 MainActivity 有这个xml文件:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout 
        android:id="@+id/rootLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <include
                android:id="@+id/tool_bar"
                layout="@layout/tool_bar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways" />
        </android.support.design.widget.AppBarLayout>

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

</android.support.v4.widget.DrawerLayout>

在我的片段中是listview,listview包含cardview。 cardview具有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/dddd"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/colorPrimary"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/fullCardItem"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bg_parent_rounded_corner">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">


            <android.support.v7.widget.Toolbar
                android:id="@+id/card_toolbar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/feed_item_profile_pic"
                android:background="@android:color/white"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                        <TextView
                            android:id="@+id/timestamp"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content" />

                </LinearLayout>
            </android.support.v7.widget.Toolbar>
        </LinearLayout>

</LinearLayout>

如何在折叠后隐藏该工具栏?

1 个答案:

答案 0 :(得分:0)

AppBarLayout崩溃后放置你想要消失的东西。

像这样:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_done"/>

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

实例: https://github.com/xuyisheng/DesignSupportLibraryDemo

当然,请看一下:

app:layout_scrollFlags="scroll|enterAlways"