<span id="Label3"></p><p><p>English Original: Https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout</p></p><p><p>This article specifically explains and coordinatorlayout the relevant knowledge points, which is the most important and difficult part of the design support Library.</p></p><span id="OSC_h2_1"><span id="OSC_h2_1"></span></span>Overview<p><p>Coordinatorlayout implements the scrolling effect mentioned in various material DESIGN. The framework now provides several ways to work without writing animated code, Including:</p></p> <ul> <ul> <li><p>Let the floating action button slide up and down, leaving room for snackbar.</p></li> </ul> </ul><p><p></p></p> <ul> <ul> <li><p>Expand or shrink the toolbar or head to allow more space in the main content Area.</p></li> </ul> </ul><p><p></p></p> <ul> <ul> <li><p>Controls which view should expand or shrink, and its display size ratio, including the Parallax scrolling effect Animation.</p></li> </ul> </ul><p><p></p></p><span id="OSC_h2_2"><span id="OSC_h2_2"></span></span>Set up<p><p>first, Make sure you follow the instructions for using the design support Library.</p></p><span id="OSC_h2_3"><span id="OSC_h2_3"></span></span>Floating action button with Snackbar<p><p>Coordinatorlayout can be used to create a floating effect with the Layout_anchor and layout_gravity properties of a floating action button, see the Floating action button guide for Details.</p></p><p><p>When Snackbar is displayed, it often appears at the bottom of the Screen. To make room for snackbar, the floating action button needs to move up.</p></p><p><p></p></p><p><p>As long as you use Coordinatorlayout as the basic layout, the animation that moves upward is automatically generated. The floating action button has a default behavior to detect the addition of the snackbar and to have the buttons appear above the Snackbar with high animations such as snackbar.</p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"> <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.v7.widget.RecyclerView android:id= "@+id/rvtodolist" android:layout_width= "match_ Parent " android:layout_height=" Match_parent "> </android.support.v7.widget.RecyclerView> < Android.support.design.widget.floatingactionbutton &nbSp; android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_ gravity= "bottom|right" android:layout_margin= "16dp" android:src= "@mipmap/ic_launcher" app:layout_anchor= "@id/rvtodolist" app:layout_ anchorgravity= "bottom|right|end"/> </android.support.design.widget.coordinatorlayout></pre><br><span id="OSC_h2_4"><span id="OSC_h2_4"></span></span>Expansion and contraction of toolbar<br><p><p>The first thing you need to make sure is that you're not using outdated actionbar. Be sure to follow the guidelines for using toolbar as a actionbar for this article. again, Coordinatorlayout is required as the main layout container.</p></p><span style="line-height: 22.5px; font-size: 12.5px;"><span style="line-height: 22.5px; font-size: 12.5px;"></span></span> <pre class="brush:xml;toolbar: true; auto-links: false;"><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:fitssystemwindows= "true" > <android.support.v7.widget.Toolbar android:id= "@+id/toolbar" android:layout_width= "match_parent" android:layout_height= "? attr/ Actionbarsize " App:popuptheme= "@style/themeoVerlay. Appcompat.light " /> </android.support.design.widget.CoordinatorLayout></pre> <span style="line-height: 22.5px; font-size: 12.5px;"><span style="line-height: 22.5px; font-size: 12.5px;"><br>next, We must use a container layout:</span></span>Appbarlayout<span style="line-height: 22.5px; font-size: 12.5px;"><span style="line-height: 22.5px; font-size: 12.5px;">to let toolbar respond to scrolling events. </span></span>Responding to scrolling events<pre class="brush:xml;toolbar: true; auto-links: false;"><android.support.design.widget.appbarlayout android:id= "@+id /appbar " android:layout_width=" match_parent " android:layout_height= "@dimen/detail_backdrop_height" android:theme= "@style/themeoverlay.appcompat.dark.actionbar" android:fitssystemwindows= "true" > < android.support.v7.widget.toolbar android:id= "@+id/toolbar" android:layout_width= "match_parent" android:layout_height= "attr/actionBarSize" app:popuptheme= "@style/themeoverlay.appcompat.light" /> </android.support.design.widget.AppBarLayout></pre><p><p>then, we need to define the link between the Appbarlayout and the scrolling view. Add App:layout_behavior on Recyclerview or any view that supports nested scrolling, such as Nestedscrollview. The support library contains a special string resource, @string/appbar_scrolling_view_behavior, It matches the appbarlayout.scrollingviewbehavior to inform Appbarlayout when this particular view has a scrolling event, and the behavior needs to be set on the view that triggered the event (scrolling). Note: According to the official Google docs, Appbarlayout must now be the first child view nested inside the Coordinatorlayout.</p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><pre class="brush:xml;toolbar: true; auto-links: false;"><android.support.v7.widget.recyclerview android:id= "@+id/rvtodolist" android:layout_width= "match_parent" android:layout_height= "match_parent" app:layout_behavior= "@string/appbar_scrolling_view_behavior" ></pre></pre><p>The view defined in the <p> Appbarlayout has the App:layout_scrollflags attribute Set. Can be triggered at the time of the Recyclerview scrolling event: when Coordinatorlayout discovers that the attribute is defined in recyclerview, it searches for the other view it contains to see if a view is associated with the Behavior. Appbarlayout.scrollingviewbehavior describes the dependency between Recyclerview and Appbarlayout. Any scrolling event of Recyclerview will trigger the change of view inside the Appbarlayout or Appbarlayout. </p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><android.support.design.widget.appbarlayout android:layout_ Width= "match_parent" android:layout_height= "wrap_content" android:fitssystemwindows= "true" 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" app:layout_scrollflags= "scroll|enteralways"/> </ Android.support.design.widget.appbarlayout></pre>Enteralways: Once the view is scrolled up, it will be visible. The App:layout_scrollflags attribute must have at least scroll the flag enabled, so the view will scroll out of the screen or it will remain fixed at the Top. Other flags that can be used are: <ul> <ul> <li><p>Enteralwayscollapsed: <span style="font-family:‘line-height:25.6000003814697px;background-color:#FFFFFF;;color:#333333;">as the name implies, this flag defines when to enter (when it has disappeared and when it is displayed again). Assuming you define a minimum height (<span style="font-family:‘line-height:25.6000003814697px;background-color:#FFFFFF;;color:#333333;">minheight</span>) and <span style="font-family:‘line-height:25.6000003814697px;background-color:#FFFFFF;;color:#333333;">enteralways are also defined, then the view will start to show up at this minimum height, and from this point on, start slowly and unfold when scrolling to the Top. </span></span></p></li> <li><p>Exituntilcollapsed: as the name implies, this flag defines when to exit, and when you define a <span style="font-family:‘line-height:25.6000003814697px;background-color:#FFFFFF;;color:#333333;">minheight, The view disappears when the scroll reaches this minimum height. </span></p></li> </ul> </ul><p><p>Remember to put the view with scroll flag in front so that the recovered view will allow the normal exit, while the fixed view will remain at the Top.</p></p><p><p>At this point, you should notice that our toolbar is able to respond to scrolling events.</p></p><p><p></p></p><p><p>Back to Top</p></p><span id="OSC_h3_5"><span id="OSC_h3_5"></span></span>Create folding effect<p><p>If we want to create toolbar folding effect, We must put toolbar in Collapsingtoolbarlayout:</p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><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></pre> <p><p></p></p> <p><p>Now the effect is:</p></p><p><p>usually, We are setting the title of toolbar, and now we need to set the title on Collapsingtoolbarlayout instead of toolbar.</p></p><p><p><span style="font-size: 18px; font-weight: 600;"></span></p></p> <pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;">Collapsingtoolbarlayout Collapsingtoolbar = (collapsingtoolbarlayout) Findviewbyid (R.id.collapsing_toolbar); Collapsingtoolbar.settitle ("Title");</pre></pre><p><p><span style="font-size: 18px; font-weight: 600;">Creating parallax Effects</span></p></p><p><p>Back to Top</p></p><p><p>Collapsingtoolbarlayout also allows us to make more advanced animations, such as putting a imageview in it and fading out when it folds. At the same time, the height of the title will change as the user Scrolls.</p></p><p><p></p></p><p><p>To create this effect, we add a imageview that defines the app:layout_collapsemode= "parallax" Property.</p></p><span style="line-height: 22.5px; font-size: 12.5px;"><span style="line-height: 22.5px; font-size: 12.5px;"></span></span> <pre class="brush:xml;toolbar: true; auto-links: false;"> <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 &nBsp; android:id= "@+id/toolbar" android:layout_width= "match_parent" android:layout_height= "attr/actionbarsize" app:layout_scrollflags= "scroll|enterAlways" ></android.support.v7.widget.Toolbar> < Imageview android:src= "@ Drawable/cheese_1 " app: layout_scrollflags= "scroll|enteralways|enteralwayscollapsed" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:scaletype= "centercrop" app:layout_collapsemode= "parallax" android:minheight= "100dp"/> </android.support.design.widget.CollapsingToolbarLayout></pre><span id="OSC_h2_6"><span id="OSC_h2_6"></span></span><span style="line-height: 22.5px; font-size: 12.5px;"><span style="line-height: 22.5px; font-size: 12.5px;"><br>In the</span></span>Coordinatorlayout and Floating action buttons<span style="line-height: 22.5px; font-size: 12.5px;"><span style="line-height: 22.5px; font-size: 12.5px;">We discussed an example of a custom Behavior. Note: translation</span></span>Http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0718/3197.html<span style="line-height: 22.5px; font-size: 12.5px;"><span style="line-height: 22.5px; font-size: 12.5px;">. </span></span><span id="OSC_h2_7"><span id="OSC_h2_7"></span></span>Custom behavior<br><p><p>Coordinatorlayout works by searching for a child view that defines the Coordinatorlayout Behavior, whether by using App:layout_ in XML Behavior tags also add annotations by using the @defaultbehavior modifier on the view class in your Code. When scrolling occurs, coordinatorlayout attempts to trigger the child view that declares the Dependency.</p></p><p><p>To define Coordinatorlayout Behavior yourself, you need to implement the Layoutdependson () and ondependentviewchanged () two methods. appbarlayout.behavior, for example, defines these two key methods. This behavior is used to make appbarlayout change when scrolling occurs.</p></p> <pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar:true; auto-links:false;">public boolean layoutdependson ( Coordinatorlayout parent, view child, view dependency) { return dependency instanceof appbarlayout;} public boolean ondependentviewchanged (coordinatorlayout parent, view child, view dependency) { // check the behavior triggered android.support.design.widget.CoordinatorLayout.Behavior behavior = ( Android.support.design.widget.CoordinatorLayout.LayoutParams) dependency.getlayoutparams ()). Getbehavior () ; if (behavior instanceof appbarlayout.behavior) { // do stuff here }} </pre></pre><p><p>The best way to understand how to implement these custom Behavior is to study Appbarlayout.behavior and Floatingactionbuttion.behavior. Although these sources are not yet available, you can use the Android Studio 1.2 integrated anti-compiler to view Them.<br></p></p><p><p>Reference: android Material Design Compatibility library</p></p><p><p>Android M--coordinatorlayout</p></p></span>
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