向下滚动时隐藏AppBar

时间:2016-02-03 10:03:28

标签: android android-layout android-toolbar android-appbarlayout

我的布局中有AppBar和水平线性布局(包括编辑文字和两个图像按钮)和其他内容。当用户向下滚动时,我想要AppBar(实际上,Toolbar隐藏。这就是我尝试过的,appbar并不隐藏它只是停留在那里我跟着Chris Banes Cheesesquare Sample

以下是我的应用的屏幕截图:

enter image description here

当用户滚动时,我希望AppBar/Toolbar消失,并且包含edittext的水平布局会替换appbar并保留在那里。

你能告诉我我做错了吗?

<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_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"> 

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    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/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            xmlns:android="http://schemas.android.com/apk/res/android"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            />

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

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:showIn="@layout/activity_show" tools:context="com.example.bimpc1.sozluk.GosterActivity"
            android:background="@color/white"
            android:id="@+id/mylin">

        <View
            android:layout_width="fill_parent"
            android:layout_height="30dp">
        </View>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/topLayout"
            android:layout_alignParentTop="true"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <ImageButton
                android:id="@+id/btn_sil"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:gravity="center"
                android:src="@drawable/delete"
                android:background="@color/white"
                android:paddingRight="10dp"
                android:paddingLeft="10dp"
                android:paddingTop="0dp"
                android:paddingBottom="15dp"
                />

            <EditText
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/et_word"
                android:ems="12"
                android:background="@android:color/transparent"/>

            <ImageButton
                android:id="@+id/btn_getir"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:gravity="center"
                android:src="@drawable/search"
                android:background="@color/white"
                android:paddingRight="10dp"
                android:paddingLeft="10dp"
                android:paddingTop="0dp"
                android:paddingBottom="15dp"
                />

        </LinearLayout>

        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:layout_below="@+id/topLayout"
        xmlns:app="http://schemas.android.com/apk/res-auto">
            <!--many views inside scrollview..... -->

        </ScrollView>

</LinearLayout>

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

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

7 个答案:

答案 0 :(得分:40)

实际上,这种设计似乎是错误的。为什么?让我向你解释一下。

除了那些xmlns:android="http://schemas.android.com/apk/res/android"不必要或使用android:layout_alignParentTop="true" LinearLayout或使用ScrollView内容或其他内容的app:layout_behavior="@string/appbar_scrolling_view_behavior"之外,好像你不喜欢我知道发生了什么。(没问题)。

注意:最重要的是,添加:

exitUntilCollapsed这里也提到了:

http://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

此标志应隐藏该部分:(app:layout_scrollFlags="scroll|exitUntilCollapsed"

exitUntilCollapsed:
  

Toolbar设置滚动标志后,向下滚动即可   通常会导致整个内容移动。

最后:http://i.stack.imgur.com/qf1So.gif

希望这就是您所寻找的,如果不是,请编辑您的问题并添加一些截图。

随意更改标记或添加新标记以实现您的确切需要。

<强>更新

如果你想在折叠后隐藏CollapsingToolbarLayout(红色部分),只需在app:layout_scrollFlags="scroll|snap" 中使用它:

<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:fitsSystemWindows="true">

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

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <!-- Your Scrollable contents should be here - such as,
            ViewPager or etc -->

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:scrollbarSize="15sp"
                android:text="You Contents" />

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

        <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.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsingtoolbarly"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|snap">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/my_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:elevation="4dp"
                    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="190dp"
                    android:minHeight="190dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/header"
                    app:layout_collapseMode="parallax" />


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

            <LinearLayout
                android:id="@+id/mylin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <View
                    android:layout_width="fill_parent"
                    android:layout_height="30dp" />

                <LinearLayout
                    android:id="@+id/topLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <ImageButton
                        android:id="@+id/btn_sil"
                        android:layout_width="45dp"
                        android:layout_height="45dp"
                        android:background="@drawable/ic_arrow_drop_up_black_24dp"
                        android:gravity="center"
                        android:paddingBottom="15dp"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:paddingTop="0dp" />

                    <EditText
                        android:id="@+id/et_word"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:ems="12"
                        android:gravity="center" />

                    <ImageButton
                        android:id="@+id/btn_getir"
                        android:layout_width="45dp"
                        android:layout_height="45dp"
                        android:background="@drawable/ic_arrow_drop_up_black_24dp"
                        android:gravity="center"
                        android:paddingBottom="15dp"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:paddingTop="0dp" />

                </LinearLayout>

            </LinearLayout>

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


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

    <!-- And finally, NavigationView -->

    <!-- <android.support.design.widget.NavigationView
          android:id="@+id/nav_view"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          app:headerLayout="@layout/app_header"
          app:insetForeground="@color/app_color_primary_dark"
          app:menu="@menu/navigation_menu" /> -->

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

最后,您将得到相同的结果(类似googleplay design)。

<强>代码:

<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:fitsSystemWindows="true">

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

        <LinearLayout
            android:id="@+id/mylin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <View
                android:layout_width="fill_parent"
                android:layout_height="30dp" />

            <LinearLayout
                android:id="@+id/topLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageButton
                    android:id="@+id/btn_sil"
                    android:layout_width="45dp"
                    android:layout_height="45dp"
                    android:background="@drawable/ic_arrow_drop_up_black_24dp"
                    android:gravity="center"
                    android:paddingBottom="15dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="0dp" />

                <EditText
                    android:id="@+id/et_word"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:ems="12"
                    android:gravity="center" />

                <ImageButton
                    android:id="@+id/btn_getir"
                    android:layout_width="45dp"
                    android:layout_height="45dp"
                    android:background="@drawable/ic_arrow_drop_up_black_24dp"
                    android:gravity="center"
                    android:paddingBottom="15dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="0dp" />

            </LinearLayout>

        </LinearLayout>

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <!-- Your Scrollable contents should be here - such as,
            ViewPager or etc -->

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:scrollbarSize="15sp"
                android:text="You Contents" />

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

        <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/my_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:elevation="4dp"
                app:layout_scrollFlags="scroll|enterAlways"
                android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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


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

    <!-- And finally, NavigationView -->

    <!-- <android.support.design.widget.NavigationView
          android:id="@+id/nav_view"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          app:headerLayout="@layout/app_header"
          app:insetForeground="@color/app_color_primary_dark"
          app:menu="@menu/navigation_menu" /> -->

</android.support.v4.widget.DrawerLayout>
折叠后

结果:searchittext不会隐藏:

enter image description here

更新:新方式:)

List

<强>结果:

enter image description here

答案 1 :(得分:3)

我希望本文能为您提供帮助:https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

尝试使用CoordinatorLayout和CollapsingToolbar。

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways">    
        </android.support.v7.widget.Toolbar>

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

答案 2 :(得分:2)

检查一下你需要在CoordinatorLayout和CollapsingToolbar中为它设置标志,

  <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.support.v4.widget.NestedScrollView
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             app:layout_behavior="@string/appbar_scrolling_view_behavior">

         <!-- Your scrolling content -->

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

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

         <android.support.v7.widget.Toolbar
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

         <android.support.design.widget.TabLayout
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

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

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

http://developer.android.com/reference/android/support/design/widget/AppBarLayout.html

答案 3 :(得分:2)

你需要像这样构建你的布局:

<android.support.design.widget.CoordinatorLayout >
    <android.support.design.widget.AppBarLayout >
        <android.support.design.widget.CollapsingToolbarLayout >
            <ImageView />
            <android.support.v7.widget.Toolbar />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <!-- Your scrollable content here -->
</android.support.design.widget.CoordinatorLayout>

完成教程:http://blog.grafixartist.com/toolbar-animation-with-android-design-support-library/

答案 4 :(得分:2)

Drawer.Layout添加android:fitsSystemWindows="true"

<android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout"
    .../...
    // add in
    android:fitsSystemWindows="true">

最好将您的疑虑分开。使用chrisbanes/cheesesquare提供的示例,您的布局将更像这样:

<android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout"
    .../...
    // add in
    android:fitsSystemWindows="true">

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

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

    <android.support.v7.widget.Toolbar 
        // Why are you using two themes?
        // android:theme="@style/ThemeOverlay.AppCompat.ActionBar"/>

</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.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>

替换它:

    <LinearLayout .../...>
    <View .../...>
    </View>
    <LinearLayout .../...>
        <ImageButton
            .../...
            />
    </LinearLayout>
    <ScrollView .../...>
    <!--many views inside scrollview..... -->
    </ScrollView>
    </LinearLayout>

使用

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

然后使用ViewPager填充列表。

 ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    if (viewPager != null) {
        setupViewPager(viewPager);
    }

private void setupViewPager(ViewPager viewPager) {
    Adapter adapter = new Adapter(getSupportFragmentManager());
    adapter.addFragment(new CheeseListFragment(), "Category 1");
    adapter.addFragment(new CheeseListFragment(), "Category 2");
    adapter.addFragment(new CheeseListFragment(), "Category 3");
    viewPager.setAdapter(adapter);
}

然后,您可以格式化此CheeseListFragment以单独添加图像视图。

也无需继续声明您的架构:

xmlns:app="http://schemas.android.com/apk/res-auto"

这表示您正在复制和粘贴代码而不理解它。

您还应该使用不带视图的nestedScrollView,以及它上面的线性布局。请注意,目前还不清楚你想要实现的目标。

答案 5 :(得分:1)

为了隐藏工具栏,请使用下面的代码。

toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();

答案 6 :(得分:1)

尝试此代码:使用如下所示的layout_scrollFlags:

  

应用程式:layout_scrollFlags = “滚动| enterAlways”

并且您的XML文件更改如下:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MainActivity">

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

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

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

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

<include layout="@layout/content_main" />

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

这是content_main XML文件

<android.support.v4.widget.NestedScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

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


</LinearLayout>

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