Design Support Library: Coordinator layout and Behavior

There are some examples for coordinator layout move floating action button upward while snackbar showing, but do you ever curious about how the whole layout interact with snackbar?
This is the example for the case. Just show how we can use Behavior to customize view intraction we want. Or, be more creative, you may have different needs.

Screenshot.gif

Step 1. Implement new behavior

It's needed to check if the operating device supports snackbar.

public class MoveUpwardBehavior extends CoordinatorLayout.Behavior<View> {
    private static final boolean SNACKBAR_BEHAVIOR_ENABLED;

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return SNACKBAR_BEHAVIOR_ENABLED && dependency instanceof Snackbar.SnackbarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    }

    static {
        SNACKBAR_BEHAVIOR_ENABLED = Build.VERSION.SDK_INT >= 11;
    }
}

Step 2. Implement custom view so that we can apply MoveUpwardBehavior to it.

In this case, we make the whole LinearLayout interact with snackbar.
It's very easy, as here said, just pass the class to DefaultBehavior annotation.

@CoordinatorLayout.DefaultBehavior(MoveUpwardBehavior.class)
public class CustomLinearLayout extends LinearLayout {
    public CustomLinearLayout(Context context) {
        super(context);
    }

    public CustomLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

Step 3. Almost Done!

Add CustomLinearLayout into layout. Remember, it should be included by CoordinatorLayout!

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.example.alisondemo.musicgenre.CustomLinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        ...
     </com.example.alisondemo.musicgenre.CustomLinearLayout>
</android.support.design.widget.CoordinatorLayout>

But....why we don't even have to implement behavior for FAB?

Form the source code of android.support.design.widget.FloatingActionButton class, as you can see:

@DefaultBehavior(FloatingActionButton.Behavior.class)
public class FloatingActionButton extends ImageView {
...

Yes, it actually has it's own Behavior implemented.

Conclusion

We can implement any kind of behavior we want in any views. It should be very interesting. :)

Full source code is now on GitHub

https://github.com/Alishuang/MusicGenre