Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

Add default behavior with Snakebars (FloatingActionButton ) #395

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemClock;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
Expand All @@ -36,6 +39,7 @@
import android.widget.ImageButton;
import android.widget.TextView;

@CoordinatorLayout.DefaultBehavior(FloatingActionButton.Behavior.class)
public class FloatingActionButton extends ImageButton {

public static final int SIZE_NORMAL = 0;
Expand Down Expand Up @@ -1317,4 +1321,37 @@ public void setLabelTextColor(int color) {
public void setLabelTextColor(ColorStateList colors) {
getLabelView().setTextColor(colors);
}

/**
* Behavior designed for use with {@link FloatingActionButton} instances. Its main function
* is to move {@link FloatingActionButton} views so that any displayed {@link Snackbar}s do
* not cover them.
*/
public static class Behavior extends CoordinatorLayout.Behavior<FloatingActionButton> {
public Behavior(){
super();
}

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

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

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

@Override
public void onDependentViewRemoved(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
ViewCompat.animate(child).translationY(0).start();

}
}
}