Pular para o conteúdo principal

CoordinatorLayout in Android - A Powerful Layout Manager

As an Android developer, you're probably aware that designing complex UIs can be a nightmare. Fortunately, CoordinatorLayout can help you manage your layouts with ease.

What is CoordinatorLayout?

CoordinatorLayout is a powerful layout manager introduced in the Android Support Library. It's designed to coordinate the interactions between various UI elements, such as scrolling, animations, and touch events.

The key feature of CoordinatorLayout is the ability to define a hierarchy of child views, each with its own behavior. This allows you to create complex UIs that respond to user input in a flexible and intuitive way.

How to Use CoordinatorLayout

To use CoordinatorLayout in your Android app, you need to add the following dependency to your build.gradle file:

implementation 'com.android.support:design:28.0.0'

Once you've added the dependency, you can start using CoordinatorLayout in your layout files. Here's an example:

<?xml version="1.0" encoding="utf-8"?>
<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 child views go here -->

</android.support.design.widget.CoordinatorLayout>

As you can see, the syntax is similar to other layout managers, such as LinearLayout or RelativeLayout. The only difference is that you're using android.support.design.widget.CoordinatorLayout instead of the standard LinearLayout or RelativeLayout.

Behaviors in CoordinatorLayout

The real power of CoordinatorLayout comes from the ability to define behaviors for each child view. A behavior is a class that extends the CoordinatorLayout.Behavior class and defines how the view should respond to user input.

For example, you can define a behavior that makes a view scroll in response to a user gesture. Here's an example:

public class ScrollBehavior extends CoordinatorLayout.Behavior<View> {

@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int axes) {
return (axes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
}

@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
child.scrollBy(0, dy);
}
}

In this example, we're defining a behavior for a view that scrolls vertically. The onStartNestedScroll method checks if the user is scrolling vertically and returns true if they are. The onNestedPreScroll method is called when the user starts scrolling, and it scrolls the child view by the amount of the user's scroll.

You can assign this behavior to a child view in your layout file like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/my_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello, World!"
app:layout_behavior=".ScrollBehavior" />

</android.support.design.widget.CoordinatorLayout>

Here, we're assigning the ScrollBehavior to a TextView, which will now scroll vertically in response to user input.

Conclusion

CoordinatorLayout is a powerful layout manager that can help you create complex UIs with ease. By defining behaviors for each child view, you can create responsive and intuitive interfaces that respond to user input in a natural way. With a little practice, you'll be able to use CoordinatorLayout to create stunning UIs that your users will love.

_

CoordinatorLayout API Definition

CoordinatorLayout was introduced in the API Level 24.1.0 and belongs to Maven artifact com.android.support:coordinatorlayout:latest_version.

As a class it derives from ViewGroup and implements the NestedScrollingParent2 interface.

public class CoordinatorLayout
extends ViewGroup implements NestedScrollingParent2

Here's inheritance hierarchy:

java.lang.Object
android.view.View
android.view.ViewGroup
android.support.design.widget.CoordinatorLayout

Behaviors

Behavior with regards to CoordinatorLayout is a interaction behavior plugin for child views of CoordinatorLayout.

public static abstract class CoordinatorLayout.Behavior
extends Object

Here's it it's inheritance tree:

java.lang.Object
android.support.design.widget.CoordinatorLayout.Behavior<V extends android.view.View>

A Behavior implements one or more interactions that a user can take on a child view. These interactions may include drags, swipes, flings, or any other gestures.

Since the introduction of CoordinatorLayout, a couple of new behaviors got implemented by developers. Then Framework designers implemented some of them in the design support library.

From BottomSheetBehavior to AnchorSheetBehavior The new Coordinator Layout from the support library gave the developers a big toy to play with. Since then, many new behaviors popped up. Most of them were included in the new design support library.

Some of those behaviors include:

  1. BottomSheetBehavior - a panel that slides from the bottom showing some content.
  2. AnchoredBehavior