Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b52eacbc authored by Justin Klaassen's avatar Justin Klaassen
Browse files

Implement proper SnackbarSlidingBehavior

Bug: 29113759

The previous LinearLayoutWithSnackbarBehavior did not implement the
all important onLayoutChild method which is responsible for intializing
the position of the child view based on its dependencies.

Change-Id: I0986601090866bfcf6f830172166796db6d9eaec
parent d7794711
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@
        android:layout_gravity="bottom"
        android:baselineAligned="false"
        android:orientation="horizontal"
        app:layout_behavior="com.android.deskclock.widget.toast.LinearLayoutWithSnackbarBehavior">
        app:layout_behavior="com.android.deskclock.widget.toast.SnackbarSlidingBehavior">

        <FrameLayout
            android:layout_width="0dp"
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -17,30 +17,52 @@
package com.android.deskclock.widget.toast;

import android.content.Context;
import android.support.annotation.Keep;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import java.util.List;

/**
 * LinearLayout Behavior that slides up and down when snackbar appears/disappears.
 * Custom {@link CoordinatorLayout.Behavior} that slides with the {@link Snackbar}.
 */
public final class LinearLayoutWithSnackbarBehavior
        extends CoordinatorLayout.Behavior<LinearLayout> {
@Keep
public final class SnackbarSlidingBehavior extends CoordinatorLayout.Behavior<View> {

    public LinearLayoutWithSnackbarBehavior(Context context, AttributeSet attrs) {}
    public SnackbarSlidingBehavior(Context context, AttributeSet attrs) {
    }

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

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, LinearLayout child,
            View dependency) {
        final float translationY = Math.min(0, dependency.getY() - child.getBottom());
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        updateTranslationY(parent, child);
        return false;
    }

    @Override
    public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
        updateTranslationY(parent, child);
    }

    @Override
    public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {
        updateTranslationY(parent, child);
        return false;
    }

    private void updateTranslationY(CoordinatorLayout parent, View child) {
        float translationY = 0f;
        for (View dependency : parent.getDependencies(child)) {
            if (parent.doViewsOverlap(child, dependency)) {
                translationY = Math.min(translationY, dependency.getY() - child.getBottom());
            }
        }
        child.setTranslationY(translationY);
        return true;
    }
}