Coordinatorlayout is so easy to use (Android)

I have found some tutorials about coordinatorlayout on the Internet. Most articles introduce coordinatorlayout, appbarlayout, collapsing toolbarlayout and toolbar together, which makes me unconsciously think that these layouts should be used together and only in that scenario. In fact, the function of coordinatorlayout is not limited to being used together with appbarlayout. Its function is powerful. This paper mainly introduces the use of coordinatorlayout, and then writes an article to integrate appbarlayout and collapsing toolbarlayout into coordinatorlayout. So how useful is coordinatorlayout? Please feel it~

What can coordinatorlayout do

Before learning coordinatorlayout, it is necessary to understand what coordinatorlayout can help us. From the name, it can be seen that it helps us coordinate the sub view. What kind of coordination method? It helps us coordinate the layout of each sub view according to our customization. Let's look at a set of animation first~

Explain the animation a little. The blue rectangle is our ordinary view, and the Yellow Hello is a button. When we drag the blue rectangle horizontally, the yellow button moves in the opposite direction to the blue rectangle; When the blue rectangle is moved vertically, the yellow rectangle is also vertical. In short: they move synchronously in the vertical direction and opposite in the horizontal direction.

If you don't need coordinatorlayout to implement this effect, there should be no problem, but the coupling degree of the code should be very large. Your code must hold two view references, and then make various judgments in ontouchevent. If the function we want to achieve is that more views should respond to the movement of the blue view, we have to process various logic for other views in the ontouchevent of the blue view. This coupling is too sad~

Since coordinatorlayout claims to help us coordinate the layout of subviews, let's see how coordinatorlayout is implemented~

Coordinatorlayout uses

The core of coordinatorlayout is behavior, which is to execute your customized actions. Before we talk about behavior, we must understand two concepts: child and dependency. What do you mean? Child, of course, means child view. Whose child view is it? Of course, it is the child view of coordinatorlayout; In fact, child refers to the child view of the coordinatorlayout to perform the action. Dependency refers to the view that the child depends on. For example, in the GIF diagram above, the blue view is dependency and the Yellow view is child, because the action of the Yellow view depends on the blue view. In short, if the dependency view changes, the child view will change accordingly. What is the specific change? Here we need to introduce behavior. The specific execution code of child changes is placed in the behavior class.

How to use behavior? First, we define a class that inherits coordinatorlayout Behavior < T >, where the generic parameter t is the view class we want to perform the action, that is, child. Then there are two ways to implement behavior:

With the above concept, let's see how to implement it~

In order to respond to the operation of following the finger, we define a very simple view. This view only responds to following the finger and takes this view as dependency. Because it is too simple, the view source code is not pasted. We only need to know the class name of the view: tempview.

Let's look at the use of behavior:

OK, now we have specified the dependency for the button class and defined the behaviors that follow the dependency. Next, we need to specify which specific button instance to bind these. The method is very simple. Just specify it directly in the layout file:

Is it simple? We just need to focus on the preparation of behavior and completely decouple the relationship between child and dependency~

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
分享
二维码
< <上一篇
下一篇>>