An example of coordinatorlayout usage of Android features

When I create a new module on the as, the default outermost layout of the system is no longer one of the five familiar layouts, but a new layout: coordinatorlayout. It is an important component of material style. As the top-level control of layout, it coordinates other components to achieve linkage.

Let's take a look at the simplest example, the use of coordinatorlayout and floatingactionbutton, which can move the floating button up and down to make room for snackBar display.

The defined layout files are as follows:

The code is simple as follows:

Get the floatingactionbutton through findviewbyid, set the click event, and let the snackBar display in onclick. Then, the rendering is like the following:

Next, look at the effect of an advanced point, that is, the title bar, that is, the expansion and contraction effect of the toolbar. In order for the toolbar to respond to scrolling events, we need to use a control: appbarlayout, which must be the direct child view of coordinatorlayout to respond to scrolling events. First, because our toobar is a view that needs to respond to scrolling, we need to configure a property for it: layout_ scrollFlags。 Then, we need to define appbarlayout and scrolling views (such as recyclerview, nestedscrollview and other controls that can support nested scrolling). The supportlibrary contains a special string resource @ string / AppBar_ scrolling_ view_ Behavior, which matches appbarlayout.scrollingviewbehavior, is used to inform appbarlayout when a scrolling event occurs in this special view. This behavior needs to be set on the view that triggers the event (scrolling). The final layout is as follows:

Main layout (activity_main. XML):

appbar_ Main.xml layout:

content_ Main.xml layout:

The renderings are as follows:

Through the effect display, when the view is scrolling, the toolbar rolls out of the screen, leaving more space for the content area. The property that controls whether the toolbar can roll out of the screen is determined by app: layout_ scrollFlags="scroll|enterAlways"。 Let's talk about this property. To get out of the screen layout_ Scrollflags must be set. The remaining flags are explained as follows:

Enterways: as long as the scroll view scrolls down, the view will be displayed.

Enteralways collapsed: as the name suggests, this flag defines when to enter (when to display again after it has disappeared). Assuming that you have defined a minimum height and enteralways, the view will start to display when it reaches the minimum height, and expand slowly from this time. When it scrolls to the top, it will be expanded.

Exituntilcollapsed: as the name suggests, this flag defines when to exit. When you define a minheight, the view will disappear when the scroll reaches the minimum height.

Next, when the flag is exituntilcollapsed, the folding display effect of the toolbar is realized. At this time, we put the toolbar directly under collapsingtoolbarlayout and modify the AppBar first_ The layout of main.xml is as follows:

design sketch:

Now let's take a look at a more fun and advanced effect to realize the feeling of poor students' poor vision when sliding. Look at the renderings first:

AppBar first_ The layout of main.xml is as follows:

The attribute of poor vision mainly comes from App: layout_ Collapsemode = "parallax", this flag represents parallax mode, that is, parallax folding effect will occur when folding, and "pin", fixed mode, is fixed at the top at the end of folding.

So much has been said above. In fact, the realization of these effects is inseparable from one thing, that is, behavior. The working principle of coordinatorlayout is to search the sub views that define the coordinatorlayout behavior, whether by using app: Layout in XML_ The behavior tag is also annotated by using the @ defaultbehavior modifier on the view class in the code. When scrolling occurs, the coordinatorlayout attempts to trigger those child views that declare dependencies. To define the coordinatorlayoutbehavior yourself, you need to implement two methods: layoutdependson () and ondependentviewchanged ().

To sum up, the difference is the various effects of coordinatorlayout.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>