-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMaterialButtonBehavior.java
More file actions
192 lines (159 loc) · 5.94 KB
/
MaterialButtonBehavior.java
File metadata and controls
192 lines (159 loc) · 5.94 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package <your package>;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.design.button.MaterialButton;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import java.util.HashMap;
import java.util.Map;
/**
* Created by ImanX. and developed by rezatutor475
*/
public class MaterialButtonBehavior extends CoordinatorLayout.Behavior<MaterialButton> {
private ValueAnimator valueAnimator = new ValueAnimator();
private int expandWidth;
private int collapseWidth;
private boolean isExpanded = true;
private int animationDuration = 170;
private final Map<MaterialButton, Integer> buttonOriginalWidths = new HashMap<>();
public MaterialButtonBehavior() {}
public MaterialButtonBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull MaterialButton child,
@NonNull View directTargetChild, @NonNull View target, int axes, int type) {
return true;
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, MaterialButton child, View dependency) {
if (expandWidth == 0 && child.getMeasuredWidth() != 0) {
expandWidth = child.getMeasuredWidth();
buttonOriginalWidths.put(child, expandWidth);
}
if (collapseWidth == 0 && child.getMinWidth() != 0) {
collapseWidth = child.getMinWidth();
}
return dependency instanceof RecyclerView;
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, MaterialButton child, View target,
int dxConsumed, int dyConsumed, int dxUnconsumed, int dy) {
if (dyConsumed < 0 && !isExpanded) {
animateButtonWidth(child, expandWidth);
isExpanded = true;
} else if (dyConsumed > 0 && isExpanded) {
animateButtonWidth(child, collapseWidth);
isExpanded = false;
}
}
private void animateButtonWidth(final View view, int targetWidth) {
if (valueAnimator != null && valueAnimator.isRunning()) return;
int currentWidth = view.getMeasuredWidth();
valueAnimator = ValueAnimator.ofInt(currentWidth, targetWidth);
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.setDuration(animationDuration);
valueAnimator.addUpdateListener(anim -> {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = (int) anim.getAnimatedValue();
view.setLayoutParams(params);
});
valueAnimator.start();
}
public void expand(MaterialButton button) {
animateButtonWidth(button, expandWidth);
isExpanded = true;
}
public void collapse(MaterialButton button) {
animateButtonWidth(button, collapseWidth);
isExpanded = false;
}
public void toggle(MaterialButton button) {
if (isExpanded) {
collapse(button);
} else {
expand(button);
}
}
public void instantExpand(MaterialButton button) {
setButtonWidth(button, expandWidth);
isExpanded = true;
}
public void instantCollapse(MaterialButton button) {
setButtonWidth(button, collapseWidth);
isExpanded = false;
}
public void setButtonWidth(MaterialButton button, int width) {
ViewGroup.LayoutParams params = button.getLayoutParams();
params.width = width;
button.setLayoutParams(params);
}
public void calibrateWidth(MaterialButton button) {
if (isExpanded) {
expandWidth = button.getMeasuredWidth();
} else {
collapseWidth = button.getMeasuredWidth();
}
}
public boolean isExpanded() {
return isExpanded;
}
public void setAnimationDuration(int duration) {
this.animationDuration = duration;
}
public int getAnimationDuration() {
return animationDuration;
}
public void show(MaterialButton button) {
button.setVisibility(View.VISIBLE);
}
public void hide(MaterialButton button) {
button.setVisibility(View.INVISIBLE);
}
public void fadeIn(MaterialButton button, int duration) {
button.setAlpha(0f);
button.setVisibility(View.VISIBLE);
button.animate().alpha(1f).setDuration(duration).start();
}
public void fadeOut(MaterialButton button, int duration) {
button.animate().alpha(0f).setDuration(duration)
.withEndAction(() -> button.setVisibility(View.INVISIBLE)).start();
}
public void toggleVisibility(MaterialButton button) {
if (button.getVisibility() == View.VISIBLE) {
hide(button);
} else {
show(button);
}
}
public boolean isAnimationRunning() {
return valueAnimator != null && valueAnimator.isRunning();
}
public void stopAnimation() {
if (valueAnimator != null && valueAnimator.isRunning()) {
valueAnimator.cancel();
}
}
public void saveState(MaterialButton button, SparseArray<Parcelable> container) {
button.saveHierarchyState(container);
}
public void restoreState(MaterialButton button, SparseArray<Parcelable> container) {
button.restoreHierarchyState(container);
}
public void enable(MaterialButton button) {
button.setEnabled(true);
}
public void disable(MaterialButton button) {
button.setEnabled(false);
}
public boolean isEnabled(MaterialButton button) {
return button.isEnabled();
}
}