I have found some tutorials on coordinatorlayout on the Internet, and most of the articles are used to introduce Coordinatorlayout, Appbarlayout, collapsingtoolbarlayout and toolbar. , which makes me unconsciously think of these several layouts to be used together, and only for that kind of scene. In fact, the function of coordinatorlayout is not limited to use with appbarlayout, it is powerful, this article mainly on the use of Coordinatorlayout introduced, Write a later article will appbarlayout and Collapsingtoolbarlayout integration coordinatorlayout together. So in the end coordinatorlayout how easy to use, please feel down ~
What Coordinatorlayout can do?
Before learning Coordinatorlayout, it is necessary to understand what coordinatorlayout can do for us, from the name can be seen, is to help us coordinate the child view. What is the coordination method? It helps us coordinate the layout of each child view according to our customization. Let's look at a set of animated pictures first
To explain this animation a little bit, the blue rectangle is one of our normal view, and the yellow Hello is a button. When we drag the blue rectangle horizontally, the yellow button checks to move in the opposite direction to the blue rectangle, and when the blue rectangle is moved vertically, the yellow is also vertical. In short: They move in a vertical direction, in the opposite direction.
This effect if you do not need to coordinatorlayout to achieve, there should be no problem, but the code should be very coupling, your code must hold 2 view references, and then in the ontouchevent to make a variety of judgments. If the function that we want to implement is that there are more view to respond to according to the movement of the blue view, then it is necessary to deal with the logic of the other view in the ontounchevent of the Blue view. This coupling degree is too sad ~
And coordinatorlayout since the claim to help us coordinate the layout of the child view, we next see how coordinatorlayout to Achieve ~
Coordinatorlayout use
The core of Coordinatorlayout's use is Behavior,behavior is to perform your custom actions. Two concepts must be understood before speaking behavior: child and dependency, what do you mean? The child is of course the meaning of the child view, whose son view, of course, is the coordinatorlayout of the child view; In fact, the child refers to the coordinatorlayout of the action. and dependency refers to the view that the child relies on. For example, in the GIF above, the Blue view is dependency, and the yellow view is the child, because the action of the Yellow view is dependent on the blue view. In short, it is as if dependency this view has changed, then the view of the child should be changed accordingly. What changes are specific to what happens? The code that will introduce the behavior,child of the change is put in the behavior class.
How to use Behavior, first of all, we define a class that inherits Coordinatorlayout.behavior<t>, where the generic parameter T is the view class that we want to perform the action, that is, the child. Then there are two ways to implement behavior:
/**
* Determines whether the layout of the child depends on dependency
/@Override public
boolean Layoutdependson (coordinatorlayout Parent, T Child, View dependency) {
Boolean rs;
Based on logical judgement RS's value
//return False indicates that the child does not rely on dependency,ture to express dependent return
RS;
}
/**
* When the dependency changes (position, width, high), perform this function
* Returns true to indicate that the position of the child or the width of the Gaoyao has changed, otherwise return false * * * *
@Override Public
Boolean ondependentviewchanged (coordinatorlayout parent, T Child, View dependency) {
//child the specific action to be performed return
true;
}
With the above concept, we see how to achieve the specific
To respond to the movement of the fingers, we define a very simple view, which only responds to the movement of the fingers, and this view is used as a dependency. Because it is too simple, this view source code does not paste, we only need to know this view of the class name: Tempview.
Let's look at the use of behavior:
Package com.hc.studyCoordinatorLayout;
Import Android.content.Context;
Import Android.support.design.widget.CoordinatorLayout;
Import Android.util.AttributeSet;
Import Android.util.DisplayMetrics;
Import Android.view.View;
Import Android.widget.Button;
/** * Package com.hc.studyCoordinatorLayout * Created by Huachao on 2016/6/1.
* * public class Mybehavior extends coordinatorlayout.behavior<button> {private int width;
Public Mybehavior (context, AttributeSet attrs) {Super (context, attrs);
Displaymetrics display = Context.getresources (). Getdisplaymetrics ();
width = display.widthpixels; @Override public Boolean Layoutdependson (coordinatorlayout parent, Button Child, View dependency) {//If dependency is
Tempview's example shows that it is what we need dependency return dependency instanceof Tempview; The Ondependentviewchanged method @Override public Boolean ondependentviewchanged (dependency) will be executed each time the location of the change occurs. Coordinatorlayout parent, Button btn, View dependency) {//according to dependency bitPlace, set button position int top = Dependency.gettop ();
int left = Dependency.getleft ();
int x = Width-left-btn.getwidth ();
int y = top;
SetPosition (btn, x, y);
return true; } private void SetPosition (View v, int x, int y) {coordinatorlayout.marginlayoutparams layoutparams = (coordinatorlay Out.
Marginlayoutparams) V.getlayoutparams ();
Layoutparams.leftmargin = x;
Layoutparams.topmargin = y;
V.setlayoutparams (Layoutparams);
}
}
OK, now that we have specified dependency for the button class, and we have defined the actions that follow dependency to change (Behavior), we will then specify which specific button instance to bind. The method is simple, just specify it in the layout file:
<?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 "Tools: context= "Com.hc.studyCoordinatorLayout.MainActivity" > <button android:id= "@+id/btn" Wrap_content "android:layout_height=" wrap_content "android:layout_marginleft=" 300DP "android:layout_margintop=" 300DP "android:background=" #FFCC00 "android:text=" Hello "app:layout_behavior=" Com.hc.studyCoordinatorLayout.MyBehavior "/> <com.hc.studycoordinatorlayout.tempview android:layout_width=" 100DP "android:layout_height=" 100DP "android:layout_marginleft=" 300DP "android:layout_margintop=" 300DP "Android:ba
ckground= "#3366CC"/> </android.support.design.widget.CoordinatorLayout>
Is it simple? We just need to focus on the writing of behavior, and completely decouple the relationship between child and dependency ~
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.