Android Support Library CoordinatorLayout Real Case Analysis

Android Support Library CoordinatorLayout Real Case Analysis CoordinatorLayout is a layout control in Android Support Library that can be used to coordinate and control the interactive behavior of sub -views.Through CoordinatorLayout, we can easily achieve a variety of complex interactive effects, such as responding to rolling events, display and hiding of the suspension button, position adjustment of sub -view, etc. This article will introduce a practical case to help readers better understand and use CoordinatorLayout.In the case, we will implement a page containing the title bar, suspended buttons, and rolling content. The suspended button will be displayed and hidden according to changes in the scroll position. First, in the XML layout file, we can organize the layout relationship of each sub -view through CoordinatorLayout.For example: <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-title bar-> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" ... /> <!-Suspension button-> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" ... /> <!-Rolling content-> <android.support.v4.widget.NestedScrollView ...> ... </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout> In the Java code, we need to perform some additional settings to control the display and hidden behavior of the suspended buttons.We can implement this function by setting Behavior.For example: CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); params.setBehavior(new ScrollAwareFabBehavior()); // ... public class ScrollAwareFabBehavior extends FloatingActionButton.Behavior { public ScrollAwareFabBehavior() { super(); } @Override public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull FloatingActionButton child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) { return axes == ViewCompat.SCROLL_AXIS_VERTICAL; } @Override public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull FloatingActionButton child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) { if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) { child.hide(); } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) { child.show(); } } } In the above code, we first obtained the Layoutparams of the Suspension button and set it to our custom ScrollawareFabBehavior.In this way, when the sub -view of the coordinatorLayout occurs, the rolling event occurs, and the callback method in ScrollawareFabBehavior will be triggered. In the OnStartnestedScroll method of ScrolllawareFabBehavior, we determine whether the Axes of the scroll event is a longitudinal scroll to determine whether the AXES of the scroll event is to listen to the rolling event. In the OnnestedScroll method, we determine the display and hiding of the floating button based on the dyConsumed value of the scroll event.When the DyConsamed is greater than 0 and the floating button is visible, the floating button is hidden; when the DyConsamed is less than 0 and the suspended button is not visible, the suspension button is displayed. Through the above layout and code settings, we have implemented a page with a suspended buttons display and hidden effect.Readers can further customize and expand the behavior of CoordinatorLayout and Suspension button according to their needs. It is hoped that the actual cases of this article can help readers better understand and use CoordinatorLayout, and play creativity and practicality in actual development.