This repository was archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathFloatingActionButtonBehavior.java
More file actions
54 lines (42 loc) · 1.81 KB
/
FloatingActionButtonBehavior.java
File metadata and controls
54 lines (42 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.github.clans.fab;
import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.util.AttributeSet;
import android.view.View;
public class FloatingActionButtonBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> {
private int mToolbarHeight = -1;
public FloatingActionButtonBehavior() {
super();
}
public FloatingActionButtonBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout
|| dependency instanceof AppBarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
super.onDependentViewChanged(parent, fab, dependency);
if (mToolbarHeight == -1) {
mToolbarHeight = Util.getToolbarHeight(fab.getContext());
}
float translationY;
if (dependency instanceof Snackbar.SnackbarLayout) {
translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
} else {
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab
.getLayoutParams();
int famBottomMargin = lp.bottomMargin;
int height = fab.getHeight();
int distanceToScroll = height + famBottomMargin;
float ratio = (float) dependency.getY() / (float) mToolbarHeight;
translationY = - distanceToScroll * ratio;
}
fab.setTranslationY(translationY);
return true;
}
}