A Preliminary Study on CoordinatorLayout and Behaviors in Android Support Design

A Preliminary Study on CoordinatorLayout and Behaviors in Android Support Design

After the release of Android M Preview, we got a new support library - Android Design Support Library, which is used to implement Google's Material Design and provides a series of controls that meet design standards.

There are many controls, the most complex and powerful one is CoordinatorLayout. As the name suggests, it is a parent view used to organize the collaboration between its child views. CoordinatorLayout can be understood as a FrameLayout by default, and its layout method is stacked layer by layer by default.

Well, the magic of CoordinatorLayout lies in the Behavior object.

Look at the Overview of the CoordinatorLayout.Behavior object

  1. Interaction behavior plugin for child views of CoordinatorLayout.
  2.  
  3. Behavior implements one or more interactions that a user can take on a child view. These interactions may include drags, swipes, flings, or any other gestures.

It can be seen that the Behavior object is used for interaction with the subviews of CoordinatorLayout.

The Behavior interface has many methods. Let's take AppBarLayout as an example. There are two Behaviors in AppBarLayout, one for itself and the other for its sibling nodes. Let's focus on the AppBarLayout.ScrollingViewBehavior class.

Let's look at the following methods in this class

0. dependency

  1. public   boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
  2. return dependency instanceof AppBarLayout;
  3. }

This method tells CoordinatorLayout that this view depends on AppBarLayout. The father can use this method to find all the sibling nodes that this child depends on.

1. measure

  1. public   boolean onMeasureChild(CoordinatorLayout parent, View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)

This is a method in which CoordinatorLayout uses Behavior objects to measure the size of child views during the measure process.

In this method, we can use the parent.getDependencies(child); method to get the view that the child depends on, and then determine its own size by getting the size of the view that the child depends on.

2. layout

  1. public   boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection)

This method is used by subviews to layout themselves. If they depend on other views, the system will call it first.

public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency)

This method can record some location information of the dependency in this callback, use the saved information to perform calculations in onLayoutChild, and then get its own specific location.

3. Nested scroll

  1.   public   boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes)
  2.  
  3. public   void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int [] consumed)
  4.  
  5. public   void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed)
  6.  
  7. public   void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target)

Do these methods look familiar to you? I have introduced them in the article NestedScrolling in Android. These methods are exactly the methods of NestedScrollingParent, which is a proxy for CoordinatorLayout. That is, CoordinatorLayout does not process these messages by itself, but passes them to the Behavior of the child view for processing. This method is used to achieve interaction and visual coordination (layout, sliding) between views.

Summarize

You can see that CoordinatorLayout implements a layout that can be delegated to child views. This is different from the traditional ViewGroup. Child views now know each other's existence, and changes in one child view can notify another child view. What CoordinatorLayout does is to act as a communication bridge to connect different views. Use Behavior objects for communication.

Our specific implementation can refer to the official Android documentation to tell us the function of each method and rewrite it to achieve the various complex functions we want.

https://developer.android.com/reference/android/support/design/widget/…

With such a mechanism, it is more convenient and quick to realize the interaction between components.

<<:  After failure in India, Google's Android One is set to fail in Africa

>>:  10 Practical but Paranoid Java Programming Techniques

Recommend

Want to improve product retention rate? Starting from these 4 methods

Retention rate is the most important indicator to...

Ankang SEO Training: How to optimize the entire site to achieve good results?

How to better optimize the entire site is a quest...

Create a TikTok video promotion ad in 5 minutes!

Come and check out the latest TikTok Ads product ...

Whose nerves were touched by Pechoin’s “high dissemination, low conversion”?

Recently, Pechoin ’s amazing advertisement has se...

Tik Tok follower growth trend in 2021!

At the beginning of every year, short video creat...

How to explain the principles of nucleic acid testing to children?

Hello, my dear children and classmates! I am Li Y...

User Operations: How do your users make choices?

How people will choose is a very complicated ques...

App delivery growth: RTA

RTA (RealTime API), which means "real-time a...

Qianlong: Is Qianlong cabbage a cold dish? I have never seen it!

One good thing about the Internet age is that you...