Android support library coordinatorLayout in the Java library

Android Support Library provides a series of categories and methods to enhance the user interface effect and interaction of Android applications.CoordinatorLayout is a very useful layout container in Android Support Library. It can be used to coordinate the location and behavior of other sub -views to create a more complex and interactive user interface.This article will introduce the application scenarios of CoordinatorLayout in the Java class library and provide some Java code examples. 1. Location of coordinating sub -view: CoordinatorLayout can use different layout parameters, such as using the App: Layout_anchor property to anchor one view to another view.This can automatically adjust the position of the sub -view according to the position of the anchor point.The following is a simple example: <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" app:layout_anchor="@id/button2" app:layout_anchorGravity="bottom|end" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" android:layout_marginBottom="16dp" android:layout_marginEnd="16dp" /> </android.support.design.widget.CoordinatorLayout> In the above example, the button 1 is anchored above the button 2, and is located at the bottom right of the button 2.No matter how the position of the button 2 changes, the button 1 will automatically adjust its position to maintain the relative position. 2. Treatment of rolling events: CoordinatorLayout can handle the rolling event of the sub -view and automatically adjust the position of the sub -view according to the scroll behavior.You can use the app: layout_behavior attribute to specify the rolling behavior of the sub -view.The following is an example: <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <android.support.design.widget.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" /> </android.support.design.widget.AppBarLayout> <!-- Other views --> </android.support.design.widget.CoordinatorLayout> In the above example, RecyclerView will use AppBarlayout's rolling behavior for layout.When the user rolled RecyclerView, AppBarlayout and Toolbar in it will automatically adjust according to the scroll event. 3. Response sliding gesture: CoordinatorLayout can be used to implement functions related to sliding gestures, such as hiding by sliding gestures or displaying toolbars.You can use the ViewOffSethelper class to modify the position of the sub -view position through the offset of CoordinatorLayout. CoordinatorLayout coordinatorLayout = findViewById(R.id.coordinatorLayout); final View toolbar = findViewById(R.id.toolbar); final ViewOffsetHelper offsetHelper = new ViewOffsetHelper(toolbar); coordinatorLayout.setOnTouchListener(new View.OnTouchListener() { private float initialY; private float lastY; private boolean isShowing = true; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: initialY = event.getRawY(); lastY = initialY; break; case MotionEvent.ACTION_MOVE: float currentY = event.getRawY(); float deltaY = currentY - lastY; lastY = currentY; offsetHelper.offsetTopAndBottom((int) -deltaY); if (isShowing && deltaY > 0) { toolbar.setVisibility(View.INVISIBLE); isShowing = false; } else if (!isShowing && deltaY < 0) { toolbar.setVisibility(View.VISIBLE); isShowing = true; } break; case MotionEvent.ACTION_UP: offsetHelper.setTopAndBottomOffset(0); break; } return true; } }); The above code fragment demonstrates how to hide or display the toolbar according to the user's sliding gesture.When the user slides upwards, the toolbar is hidden; when the user slides towards it, the toolbar is displayed again. Summarize: CoordinatorLayout is a powerful layout container provided in Android Support Library, which can be used to coordinate the location and behavior of the sub -view.Its application scenarios are very extensive, which can be used to handle the location of rolling events, coordinate sub -view positions, and can also realize functions related to sliding gestures.By using CoordinatorLayout reasonably, the interface effect and user interaction of Android applications can be improved.