Android Support Library CoordinatorLayout original analysis and source code analysis
Android support library coordinatorLayout original analysis and source code analysis
I. Overview
In Android development, CoordinatorLayout is a special Framelayout that can help us handle interaction relationships between different views and coordinate their behavior.Coordinatelayout is an important component in Android Design Support Library, which provides us with a convenient way to create complex user interfaces.
2. The characteristics of CoordinatorLayout
1. Coordinating the behavior between the child View: CoordinatorLayout can coordinate their behavior according to the rules of the child View (called Behavior). For example, when a view is hidden, other views can automatically adjust the position.
2. Provide touch feedback: By setting Behavior, you can make the child View have a touch feedback effect. For example, when a user drags a view, it can produce a dynamic effect on the screen.
3. Treatment of rolling events: CoordinatorLayout can handle the rolling event and adjust the position and size of its son view accordingly.
4. Support foldable control: CoordinatorLayout supports creation of foldable controls, such as Toolbar and CollapsingToolBarlayout.
Third, the implementation principle of coordinatorLayout
The implementation principles of CoordinatorLayout are mainly based on several important concepts: CoordinatorLayout.Layoutparams, Behavior, and CoordinatorLayout.behavior.
1. CoordinatorLayout.LayoutParams
In CoordinatorLayout, each child View uses CoordinatorLayout.Layoutparams to manage their position and attributes.There is a reference to the Behavior in LayoutParams. By Behavior, you can define the behavior of the child View.
2. Behavior
Behavior is an abstract class that defines the interactive behavior between the child View.By rewriting the method in Behavior, we can achieve custom coordinated behavior.Each child View can add a Behavior to handle the coordinated relationship between it with other sub -Views.
3. CoordinatorLayout.Behavior
CoordinatorLayout.behavior is the subclass of Behavior. It provides us with some default behaviors, such as handling rolling events and touch feedback effects.We can expand its functions by inheriting CoordinatorLayout.behavior, or directly use its subclasses such as AppBarlayout.behavior, FLOATINGACTIONBUTTON.BEHAVIVIOR, etc.
In CoordinatorLayout, the Behavior of each child View will be called so that it can make related adjustments as needed.CoordinatorLayout will coordinate their behavior by calling the corresponding methods in the Behavior of the Speed View.
Fourth, COORDINATORLAYOUT source code analysis
In the source code of CoordinatorLayout, it can be seen in its implementation principle.
1. CoordinatorLayout.onLayout()
In the onlyout () method, CoordinatorLayout will traverse all the sub -views and call their layout () method to determine their position.
2. CoordinatorLayout.onMeasure()
In the ONMEASURE () method, CoordinatorLayout will decide whether to adjust the size of the child View based on the size of the Measurespec calculator View of the child View.
3. CoordinatorLayout.Behavior.onSizeChanged()
In the Behavior's ONSizeChanged () method, Behavior can make related adjustments based on the size changes of the sub -View.For example, we can achieve the effect of foldable controls in Behavior.
4. CoordinatorLayout.Behavior.onDependentViewChanged()
In Behavior's ONDEPENDENTVIEWCHANGED method, Behavior can adjust the position and size of the child View based on the relationship with other sub -views.For example, we can achieve the effect of one child View follow the other View.
5. Example code
The following is a simple example code that demonstrates the basic usage of CoordinatorLayout.
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image"
android:scaleType="centerCrop" />
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>
This example contains an AppBarlayout, a CollapsingToolBarlayout, an imageview, a Toolbar, a RecyclerView, and a FlorationActionButton.By setting up different Behavior, the coordination effect between views can be achieved.
The above is an introduction to the original analysis and source analysis of the Android support library CoordinatorLayout.By understanding the working principle of CoordinatorLayout, we can better use this powerful layout container to create a complex user interface.