"Turn" Material Design folding effect Toolbar collapsingtoolbarlayout appbarlayout

Source: Internet
Author: User

I really like the effect of folding the toolbar in material design, Bilibili the Android Client Video detail page. In the second part of this article, we will understand the use of the next collapsingtoolbarlayout by simply imitating the implementation of the Bilibili video detail page. The third part of the article introduces the combination of Collapsingtoolbarlayout and tablayout.

Basic friends can skip the first part directly.

First, the relevant basic properties of the introduction

There is an activity template called scrollingactivity in Android Studio, which implements a simple collapsible toolbar that we add to the project.

The layout code for scrollingactivity is as follows

<?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 "    Android:fitssystemwindows= "true" ><android.support.design.widget.appbarlayout android:id= "@+id/app_bar" Android:layout_width= "Match_parent" android:layout_height= "@dimen/app_bar_height" 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= "Match_parent" android:fitssystemwindows= "true" App:contentscrim = "? Attr/colorprimary" app:layout_scrollflags= "scroll|exituntilcollapsed" > <android.support.v7.widget.t          Oolbar  Android:id= "@+id/toolbar" android:layout_width= "match_parent" android:layout_height= "? Attr/actionB Arsize "app:layout_collapsemode=" pin "app:popuptheme=" @style/apptheme.popupoverlay "/> </an droid.support.design.widget.collapsingtoolbarlayout></android.support.design.widget.appbarlayout>< Android.support.v4.widget.NestedScrollView android:layout_width= "Match_parent" a        ndroid:layout_height= "Match_parent" app:layout_behavior= "@string/appbar_scrolling_view_behavior" > <textview android:layout_width= "wrap_content" Android:layout_heigh t= "Wrap_content" android:layout_margin= "@dimen/text_margin" android:text= "@string/L Arge_text "/> </android.support.v4.widget.NestedScrollView>< Android.support.design.widget.FloatingActionButton android:id= "@+id/fab" andRoid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_margin= "@dimen/fab_margin" a ndroid:src= "@android:d rawable/ic_dialog_email" app:layout_anchor= "@id/app_bar" app:layout_anchorgravity= "bottom| End "/></android.support.design.widget.coordinatorlayout>

Appbarlayout is an app bar layout that supports response scrolling gestures (such as a toolbar roll-out or roll-in screen), and Collapsingtoolbarlayout is a layout specifically designed to respond to the scrolling details of different elements within a sub-layout.

The rolling layouts (Recyclerview, Nestedscrollview, etc.) that are combined with appbarlayout need to be set app:layout_behavior= "@string/appbar_scrolling_view_ Behavior "(as set by the Nestedscrollview control in the code above). If not set, Appbarlayout will not respond to scrolling events for scrolling layouts.

Collapsingtoolbarlayout and ScrollView use a sliding bug, and note that you want to use Nestedscrollview instead of ScrollView.

The Appbarlayout sub-layout has 5 scrolling identities (which is the App:layout_scrollflags attribute configured in the code collapsingtoolbarlayout above):

    1. Scroll: Associates This layout with the scrolling time. This identity is to be set before other identities, without which the layout does not scroll and the other identity settings are not valid.
    2. Enteralways: Any scroll-down action will make this layout visible. This identity is often referred to as "quick return" mode.
    3. Enteralwayscollapsed: Suppose you define a minimum height (minheight) and enteralways is also defined, then the view will start to show at this minimum height, and from this time on, start slowly, When scrolling to the top, unfold.
    4. Exituntilcollapsed: When you define a minheight, this layout collapses when scrolling reaches this minimum height.
    5. Snap: When a scrolling event ends, if the view is partially visible, it will be scrolled to shrink or expand. For example, if the view is only shown at bottom 25%, it will collapse. Conversely, if its bottom 75% is visible, then it will be fully expanded.

Collapsingtoolbarlayout can set the color of the toolbar layout when collapsed by App:contentscrim, by App:statusbarscrim setting the status bar when collapsed. The default Contentscrim is the colorprimary color value, Statusbarscrim is the Colorprimarydark color value. This is going to be used later.

Collapsingtoolbarlayout's sub-layout has 3 folding modes (App:layout_collapsemode set in toolbar)

    1. Off: This is the default property, the layout will be displayed normally, no folding behavior.
    2. After the pin:collapsingtoolbarlayout is folded, the layout is pinned to the top.
    3. This layout also has a parallax folding effect when the parallax:collapsingtoolbarlayout is folded.

When Collapsingtoolbarlayout's sub-layout is set to parallax mode, we can also set the Parallax scrolling factor by app:layout_collapseparallaxmultiplier with a value of: 0~1.

Floatingactionbutton This control is anchored under appbarlayout by app:layout_anchor this setting. Floatingactionbutton source has a behavior method, when appbarlayout contraction, Floatingactionbutton will follow the corresponding changes. About Coordinatorlayout and behavior, my next article will study with you.

This pile of properties look a bit annoying, you can create a new scrollingactivity template to experiment with play.

Second, imitate Bilibili client Video Details page

Let's analyze the original interface first.

The interface is initially, Collapsingtoolbarlayout is expanded, and the video cover is displayed. We scroll up the interface, collapsingtoolbarlayout contraction. When the appbarlayout is completely folded, the video AV number is hidden, showing a small TV icon and "Play Now", click to make appbarlayout fully unfold, The Collapsingtoolbarlayout sub-layout is switched from ImageView to the video pop-up player.

The amount ... Play Screen player ...

B Station very early open source a bomb screen engine, also played a crazy drag cool hanging fried days of the name called "Fire bomb Screen make" (a see is two times the work of the ape →_→), source on GitHub, the project named Danmakuflamemaster.

Let's take a look at the layout of the modified finish first.

<?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:id=" @+id /coordinatorlayout "android:layout_width=" match_parent "android:layout_height=" Match_parent ">< Android.support.design.widget.AppBarLayout android:id= "@+id/app_bar" android:layout_width= "Match_parent" Android: layout_height= "@dimen/app_bar_height" android:fitssystemwindows= "true" android:theme= "@style/ Apptheme.appbaroverlay "> <android.support.design.widget.collapsingtoolbarlayout android:id=" @+id/toolbar_l Ayout "android:layout_width=" match_parent "android:layout_height=" match_parent "app:contentscrim="? at Tr/colorprimary "app:statusbarscrim=" @android: Color/transparent "app:layout_scrollflags=" Scroll|exituntilcol Lapsed|snap "> <!--cover picture--<imageview android:id=" @+id/imaGeview "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android: Scaletype= "Centercrop" android:src= "@drawable/diqiu" app:layout_collapsemode= "Parallax" a        Pp:layout_collapseparallaxmultiplier= "0.7" android:fitssystemwindows= "true"/> <!--video and barrage controls-- <framelayout android:id= "@+id/video_danmu" android:layout_width= "Match_parent" an droid:layout_height= "Match_parent" app:layout_collapsemode= "Parallax" App:layout_collapseparallaxmul Tiplier= "0.7" android:fitssystemwindows= "true" android:visibility= "Gone" > <videovie W android:id= "@+id/videoview" android:layout_width= "Match_parent" Android:lay out_height= "Match_parent"/> <!--beep, open-source bullet screen control--&LT;MASTER.FLAME.DANMAKU.UI.WIDGET.DANMAKUV Iew AndroId:id= "@+id/danmaku" android:layout_width= "match_parent" android:layout_height= "match_parent            "/> </FrameLayout> <android.support.v7.widget.toolbar android:id=" @+id/toolbar " Android:layout_width= "Match_parent" android:layout_height= "Attr/actionbarsize" App:layout_co            Llapsemode= "Pin" app:popuptheme= "@style/apptheme.popupoverlay" > <!--Customize the Play Now button with pictures- <android.support.v7.widget.buttonbarlayout android:id= "@+id/playbutton" Android:lay                Out_width= "Match_parent" android:layout_height= "match_parent" android:gravity= "center" Android:visibility= "Gone" > <imageview android:layout_width= "wrap_content                    "Android:layout_height=" match_parent "android:layout_gravity=" Center_horizontal " Android: src= "@mipmap/ic_play_circle_filled_white_48dp"/> <textview android:layout_width= "                    Wrap_content "android:layout_height=" wrap_content "android:textcolor=" #ffffff "            Android:text= "Play Now" android:layout_gravity= "center_vertical"/> </android.support.v7.widget.ButtonBarLayout> </android.support.v7.widget.Toolbar> </androi d.support.design.widget.collapsingtoolbarlayout></android.support.design.widget.appbarlayout>< Include layout= "@layout/content_scrolling"/><android.support.design.widget.floatingactionbutton android:id = "@+id/fab" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_margin= "@dime N/fab_margin "android:src=" @mipmap/ic_play_circle_filled_white_48dp "app:layout_anchor=" @id/app_bar "app:layout_a Nchorgravity= "Bottom|end"/></android.suPport.design.widget.coordinatorlayout> 

I changed the color value of Colorprimary to B station "girl powder", playing the icon is from the Internet to find.

<color name="colorPrimary">#FA7199</color>

Because we want to implement the immersive status bar, we need to first set the entire activity to the status bar transparent mode. Then in the layout file, the Collapsingtoolbarlayout to implement immersive control settings on android:fitssystemwindows= "true", if not set, the sub-layout is located under the status bar, does not extend to the status bar.

The layout is not complicated, the next step is to achieve the function of non-bouncing screen playback.

We need to monitor the collapsingtoolbarlayout of the folding and unfolding state. Alas I went, the official did not provide a ready-made method (⊙_⊙? )。

View the source code, you can see Collapsingtoolbarlayout is through the implementation of Appbarlayout Onoffsetchangedlistener interface, The parallax movement of the sub-layout and title and the display of the Contentscrim and Statusbarscrim are implemented according to the appbarlayout offset. Then we can also call Appbarlayout's Addonoffsetchangedlistener method to monitor the appbarlayout displacement, to determine the status of Collapsingtoolbarlayout.

Write an enumeration that defines the three states of the collapsingtoolbarlayout expansion, folding, and middle.

 private CollapsingToolbarLayoutState state; private enum CollapsingToolbarLayoutState {    EXPANDED,    COLLAPSED,    INTERNEDIATE}

Next, the appbarlayout is monitored, the state of Collapsingtoolbarlayout is judged and the corresponding logic is realized.

To make everyone more intuitive about the state, I changed the title by modifying the status value.

When using collapsingtoolbarlayout, it is important to note that the Settitle () method called Toolbar after the Collapsingtoolbarlayout Setup is completed will have no effect. Instead, we need to call Collapsingtoolbarlayout's Settitle () method to modify the toolbar. (For specific reasons, please go to see the collapsingtoolbarlayout source code will know (˙-˙))

Appbarlayout app_bar= (appbarlayout) Findviewbyid (R.id.app_bar); App_bar.addonoffsetchangedlistener (new Appbarlayout.onoffsetchangedlistener () {@Override public void onoffsetchanged (appbarlayout appbarlayout, int vertic                Aloffset) {if (Verticaloffset = = 0) {if (state! = collapsingtoolbarlayoutstate.expanded) { state = collapsingtoolbarlayoutstate.expanded;//modified status marked as expanded Collapsingtoolbarlayout.settitle ("EXPANDED");            Set the title expanded}} else if (Math.Abs (verticaloffset) >= Appbarlayout.gettotalscrollrange ()) { if (state! = collapsingtoolbarlayoutstate.collapsed) {collapsingtoolbarlayout.settitle ("");//settings tit Le does not show playbutton.setvisibility (view.visible);//Hide play button state = collapsingtoolbarlayoutstate.c                ollapsed;//the modified state is marked as collapsed}} else {if ', ' collapsingtoolbarlayoutstate.internediate = ' { if (state = = Collapsingtoolbarlayoutstate.collapsed) {playbutton.setvisibility (View.gone);//Hide Play button when collapsed to intermediate state} Collapsingtoolbarlayout.settitle ("Internediate");//Set title to Internediate state = Collapsingtoolbarlayou tstate.internediate;//modification status marked as Intermediate}}});

Then set the listener on the play button and click the Appbarlayout setexpanded (True) method to make the toolbar expand.

Beep the title of the client is fixed and can be implemented by calling Collapsingtoolbarlayout's settitleenabled (false) method.

When the video is playing, calling Nestedscrollview's setnestedscrollingenabled (false) method causes the Appbarlayout to not respond to the scrolling event.

A careful friend might have found a beep. Client in order to avoid the video cover picture color too shallow affect the display of the status bar information, add a gradient of the opaque layer.

Implementing a gradient matte layer is simple. A new XML file named gradient is created under the Res/drawable folder, with the following code:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <gradient        android:startColor="#33000000"        android:endColor="#00000000"        android:angle="270" /></shape>

Shape node, you can set shapes by Android:shape, which by default is a rectangle. The value of angle in the gradient node is 270 from top to bottom, 0 from left to right, and 90 from bottom to top. The starting color #33000000 is 20% opacity of black, #00000000表示全透明.

Then add a custom view underneath the ImageView code in the Collapsingtoolbarlayout and the background is set to the gradient above.

<View   android:layout_width="match_parent"   android:layout_height="40dp"   android:background="@drawable/gradient"   android:fitsSystemWindows="true"/>    

The height of the general status bar about 20DP, I want to let the gradient effect is more natural, and not too much influence on the picture (Mei) slice (zi), the height is set to 40DP. (status bar can see, sister face is not black, very good (??.??))

I omitted the play of the relevant implementation of the screen, the next as long as the play button listening to write out the cover image of the hidden, video and barrage screen control display initialization and playback logic, in the appbarlayout of the three state monitoring according to whether the video in play write other corresponding logic is good, Interested friends can download the beep Code "fire bomb screen so that" source Danmakuflamemaster play.

B site hit after the pan or coin will appear a similar Snackbar prompt control, you can pass my previous article no time to explain, quickly use Snackbar!—— Android snackbar Fancy Use Guide to achieve, welcome friends interested to see.

It's not that I'm too lazy on the code, really ... (Base friend: hurriedly, turn black.) Me: Wait for me, come right now! \@@facesymbol@@/)

Three. Collapsingtoolbarlayout and Tablayout

The combination of Collapsingtoolbarlayout and tablayout also works well.

Look at the code in Collapsingtoolbarlayout.

<android.support.design.widget.collapsingtoolbarlayout android:id= "@+id/collapsingtoolbar" Android:layout_ Width= "Match_parent" android:layout_height= "Match_parent" app:titleenabled= "false" app:contentscrim= "@color/color Primary "app:statusbarscrim=" @android: Color/transparent "app:layout_scrollflags=" Scroll|exituntilcollapsed|snap " > <imageview android:id= "@+id/imageview" android:layout_width= "Match_parent" Android:layout_ height= "Match_parent" android:scaletype= "Centercrop" android:adjustviewbounds= "true" App:layout_colla Psemode= "Parallax" app:layout_collapseparallaxmultiplier= "0.7" android:fitssystemwindows= "true"/> &lt ; View android:layout_width= "match_parent" android:layout_height= "40DP" android:background= "@drawable/g Radient "android:fitssystemwindows=" true "/> <android.support.v7.widget.toolbar android:id=" @+id/to Olbar "Android:layout_width= "Match_parent" android:layout_height= "96DP" app:titlemargintop= "15DP" android:gravity= "Top" a    Pp:layout_collapsemode= "pin" app:title= "Hello" app:popuptheme= "@style/apptheme.popupoverlay"/>        <android.support.design.widget.tablayout android:id= "@+id/tablayout" android:layout_width= "Match_parent" android:layout_height= "45DP" android:layout_gravity= "Bottom"/></android.support.design.widget.collapsi Ngtoolbarlayout>

The tablayout does not set the App:layout_collapsemode, and it does not disappear when the collapsingtoolbarlayout shrinks.

Collapsingtoolbarlayout contraction height is the height of toolbar, so we need to increase the height of the toolbar, give the tablayout to leave a position, so that after the contraction tablayout will not and toolbar overlap.

The height of the toolbar increases, and the title moves down accordingly. The android:gravity= "Top" method causes the toolbar title to be located above the toolbar and then adjusts the title to the top height by app:titlemargintop, so that the toolbar is the same as it was originally displayed.

Collapsingtoolbarlayout can also be used in conjunction with palette, but I feel some pits in actual use, because the picture in Collapsingtoolbarlayout is uncertain, The color that palette gets from the picture is probably not what you want.

Interested friends can check the usage of palette by themselves.

"Turn" Material Design folding effect Toolbar collapsingtoolbarlayout appbarlayout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.