Android Support Library CoordinatorLayout best practice guide

Android Support Library CoordinatorLayout best practice guide CoordinatorLayout is an important component in Android Support Library that can coordinate and handle interaction and animation effects between different views.This article will explain some of the best practices to use CoordinatorLayout. 1. Add dependencies To use CoordinatorLayout, you need to add corresponding dependencies to the project's built.gradle file.Make sure your project has been configured with the support library. groovy implementation 'com.android.support:coordinatorlayout:1.1.0' 2. Use CoordinatorLayout as the root layout CoordinatorLayout should be the root element of your layout file.It can contain other views, such as Toolbar, RecyclerView or NestedScrollView. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Your layout content here --> </android.support.design.widget.CoordinatorLayout> 3. Set Behavior CoordinatorLayout controls the interactive effects between different views through Behavior.Each view can be associated with a Behavior, and the Behavior will tell CoordinatorLayout how to handle your interaction with other views. <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior" /> In the above example, Toolbar associates the AppBarlayout.scrollingViewBehavior. In this way, when the scroll interface, the coordinatorLayout will control the display and hiding of Toolbar according to the scroll position. 4. Use NestedScrolling CoordinatorLayout's support for NestedScrolling is very good, which can easily achieve complex scroll effects.NestedScrolling can be used to contain the parent view of nested scroll views (such as NestedScrollView, RecyclerView, etc.). <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"> <!-- Your scrollable content here --> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout> In the above example, the NestedScrollView as a sub -view of the CoordinatorLayout and set up the AppBarlayout.scrollingViewBehavior.In this way, when rolling NestedScrollView, CoordinatorLayout will handle the interactive effect of AppBarlayout and Toolbar. 5. Custom Behavior If you need to achieve a custom interactive effect, you can create your own behavior.Custom Behavior needs to inherit from CoordinatorLayout.behavior and rewrite some methods to handle the corresponding interaction behavior. public class CustomBehavior extends CoordinatorLayout.Behavior<View> { // Implement custom behavior here } You can associate it on the viewing of a custom Behavior by setting the layout_behavior attribute. <View android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="com.example.app.CustomBehavior" /> Through the above steps, you can use CoordinatorLayout to achieve complex layout and interactive effects.I hope this article will help you understand and use CoordinatorLayout.