简体   繁体   English

CollapsingToolbarLayout 并在滚动时隐藏工具栏

[英]CollapsingToolbarLayout and hide toolbar while scrolling

I am trying to create some combined layout using CoordinatorLayout and also CollapsingToolbarLayout.我正在尝试使用 CoordinatorLayout 和 CollapsingToolbarLayout 创建一些组合布局。

In the first state, when we on the most top page, and didn't scrolled yet, I want the toolbar to expend as shown below (yes, i did it):在第一种状态下,当我们位于最顶部且尚未滚动时,我希望工具栏如下所示展开(是的,我做到了):

在此处输入图片说明

In the second state, when starting to scroll down, the image and toolbar should disappear, as shown below (only tab will show):在第二种状态下,当开始向下滚动时,图像和工具栏应该消失,如下所示(仅显示选项卡):

在此处输入图片说明

And in the last state once I am at some point in the list (but not the top of the list) I want to start scrolling up, once I start scrolling up I want the toolbar (and not the expended one with the image) to start whowing, as shown below (if didn't reaches the top of the list, the image will not show, only the toolbar):在最后一个状态中,一旦我在列表中的某个点(但不是列表的顶部)我想开始向上滚动,一旦我开始向上滚动,我想要工具栏(而不是带有图像的工具栏)到开始whowing,如下图(如果没有到达列表顶部,则不会显示图像,只有工具栏):

在此处输入图片说明

I was able to achive the the first state, but the other 2 state are problematic, once toolbar is implemented inside CollapsingToolbarLayout, the flexability of what I can do with it outside of CollapsingToolbarLayout component is not clear.我能够实现第一个状态,但其他 2 个状态有问题,一旦在 CollapsingToolbarLayout 内部实现了工具栏,我在 CollapsingToolbarLayout 组件之外可以用它做什么的灵活性就不清楚了。 I can't make the toolbar hide, if I do so, then it will only be shown once I reaches the top.我无法隐藏工具栏,如果我这样做,它只会在我到达顶部时显示。

Anyways, my current XML (showing below) is in state where the first picture is implemented, but once I start scrolling down, the toolbar stay at the top and do not hide.无论如何,我当前的 XML(如下所示)处于实现第一张图片的状态,但是一旦我开始向下滚动,工具栏就会停留在顶部并且不会隐藏。 Note: I must tell the toolbar to stay "pin" because if I didn't then the information inside the toolbar disappear, and only an empty toolbar will show (that's for another post, but it still interesting to know why this happen?).注意:我必须告诉工具栏保持“固定”状态,因为如果我不这样做,那么工具栏中的信息就会消失,并且只会显示一个空的工具栏(这是另一篇文章,但知道为什么会发生这种情况仍然很有趣?) .

here is my current xml:这是我当前的 xml:

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

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            >

            <include
                android:id="@+id/toolbar_search_container"
                layout="@layout/search_box"
                android:layout_height="192dp"
                android:layout_width="match_parent"
                app:layout_collapseMode="parallax"

                />

            <include
                android:id="@+id/toolbar_benefit"
                layout="@layout/toolbar_main"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:contentScrim="?attr/colorPrimary"

                />

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

        <android.support.design.widget.TabLayout
            android:id="@+id/benefit_tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/primaryColor"
            app:tabIndicatorColor="@color/accentColor"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="@android:color/black"
            app:tabIndicatorHeight="4dp" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/benefit_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    <include
        layout="@layout/floating_btn_benefits"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        />
</android.support.design.widget.CoordinatorLayou

I have fixed the issue, just to clerify, I wanted my Toolbar to be able to expand with a paralex image once it reaches the top, but I also wanted the toolbar to disappear if scrolling down, and show itself again (without the paralex image) once I scroll up.我已经解决了这个问题,只是为了澄清,我希望我的工具栏能够在它到达顶部时用一个平行图像扩展,但我也希望工具栏在向下滚动时消失,并再次显示自己(没有平行图像) 一旦我向上滚动。 the paralex image effect should only displayed if I reaches the top.只有当我到达顶部时,才会显示 paralex 图像效果。

So basically the solution is, change the component CollapsingToolbarLayout with the following attribute:所以基本上解决方案是,使用以下属性更改组件CollapsingToolbarLayout

app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"

and also change toolbar component with the following attribute并使用以下属性更改工具栏组件

android:minHeight="?attr/actionBarSize"

regarding my paralex effect image (which is my toolbar_search_container ) I shouldn't add any layout_scrollFlags to it.关于我的 paralex 效果图像(这是我的toolbar_search_container ),我不应该向它添加任何layout_scrollFlags

So why is it working?那么它为什么有效呢? To understand it, you need to know what is enterAlwaysCollapsed , The enterAlwaysCollapsed effects views that added the minHeight attribute.要理解它,您需要知道什么是enterAlwaysCollapsed ,添加minHeight属性的enterAlwaysCollapsed效果视图。 this means, every child of CollapsingToolbarLayout which have minHeight will be effected by this attribute.这意味着,具有minHeightCollapsingToolbarLayout每个子项都将受此属性影响。 So my toolbar will be effected.这样我的工具栏就会生效。

enterAlwaysCollapsed attribute definition in simple words: enterAlwaysCollapsed属性定义简单来说:

Assuming enterAlways is declared and you have specified a minHeight, you can also specify enterAlwaysCollapsed .假设声明了 enterAlways 并且您指定了 minHeight,您还可以指定enterAlwaysCollapsed When this setting is used, your view will only appear at this minimum height.使用此设置时,您的视图将仅出现在此最小高度处。 Only when scrolling reaches to the top will the view expand to its full height..."只有当滚动到达顶部时,视图才会扩展到其全高......”

Well, isn't this exactly what we want?嗯,这不正是我们想要的吗? (do not answer this retorical question ;) ) (不要回答这个反驳的问题;))

One more thing to add, the parallax component ( toolbar_search_container ) is depends on the toolbar to expand, and because the toolbar will expand only when it reaches the top, then this is all just working great!还要补充一点,视差组件( toolbar_search_container )依赖于工具栏的展开,而且因为工具栏只有在到达顶部时才会展开,所以这一切都很好!

The new code is :新代码是:

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_material_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
            >

            <include
                android:id="@+id/toolbar_search_container"
                layout="@layout/search_box"
                android:layout_height="192dp"
                android:layout_width="match_parent"
                app:layout_collapseMode="parallax"
                />

            <include
                android:id="@+id/toolbar_benefit"
                layout="@layout/toolbar_main"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:minHeight="?attr/actionBarSize"
                app:contentScrim="?attr/colorPrimary"
                app:layout_collapseMode="pin"
                android:fitsSystemWindows="true"
                />

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

        <android.support.design.widget.TabLayout
            android:id="@+id/benefit_tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/primaryColor"
            app:tabIndicatorColor="@color/accentColor"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="@android:color/black"
            app:tabIndicatorHeight="4dp" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/benefit_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    <include
        layout="@layout/floating_btn_benefits"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        />
</android.support.design.widget.CoordinatorLayout>

将此行代码添加到您的 CollapsingToolbarLayout

app:layout_scrollFlags="scroll|snap|enterAlways|enterAlwaysCollapsed"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 滚动时隐藏工具栏 - hide Toolbar while scrolling 滚动时CollapsingToolbarLayout不塌陷 - CollapsingToolbarLayout not collapsing while scrolling 滚动片段工具栏应隐藏在android中 - While Scrolling Fragment toolbar should hide in android 如何在相对布局中有效滚动时隐藏工具栏? - How to hide toolbar while scrolling efficientliy in relativeLayout? CollapsingToolbarLayout滚动内的Android图像和工具栏(EditText) - Android Image and Toolbar(EditText) inside CollapsingToolbarLayout Scrolling 向下滚动时,CollapsingToolbarLayout闪烁 - CollapsingToolbarLayout flickers while scrolling down CollapsingToolbarLayout:保留工具栏但隐藏TabLayout并快速返回 - CollapsingToolbarLayout: Keep Toolbar but hide TabLayout with quick return 滚动RecyclerView时无法隐藏工具栏,可能是原因? - Unable to hide Toolbar while scrolling RecyclerView, possible cause? 工具栏不会自动隐藏,在recyclerview中滚动时需要手动按下才能隐藏 - toolbar is not hiding automatically need to push manually to hide while scrolling in recyclerview 如何在 Web 视图中向下滚动时隐藏操作栏/工具栏 - How to Hide ActionBar/Toolbar While Scrolling Down in Webview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM