aboutsummaryrefslogtreecommitdiffstats
path: root/ahbottomnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHBottomNavigationBehavior.java
blob: bebdee7e216c52617958e3d5a879b6bf45a5f28b (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package com.aurelhubert.ahbottomnavigation;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.support.v4.view.ViewPropertyAnimatorUpdateListener;
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;

/**
 *
 */
public class AHBottomNavigationBehavior<V extends View> extends VerticalScrollingBehavior<V> {

	private static final Interpolator INTERPOLATOR = new LinearOutSlowInInterpolator();
	private static final int ANIM_DURATION = 300;

	private int mTabLayoutId;
	private boolean hidden = false;
	private ViewPropertyAnimatorCompat translationAnimator;
	private ObjectAnimator translationObjectAnimator;
	private TabLayout mTabLayout;
	private Snackbar.SnackbarLayout snackbarLayout;
	private FloatingActionButton floatingActionButton;
	private int mSnackbarHeight = -1;
	private boolean fabBottomMarginInitialized = false;
	private float targetOffset = 0, fabTargetOffset = 0, fabDefaultBottomMargin = 0, snackBarY = 0;
	private boolean behaviorTranslationEnabled = true;

	/**
	 * Constructor
	 */
	public AHBottomNavigationBehavior() {
		super();
	}

	public AHBottomNavigationBehavior(boolean behaviorTranslationEnabled) {
		super();
		this.behaviorTranslationEnabled = behaviorTranslationEnabled;
	}

	public AHBottomNavigationBehavior(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AHBottomNavigationBehavior_Params);
		mTabLayoutId = a.getResourceId(R.styleable.AHBottomNavigationBehavior_Params_tabLayoutId, View.NO_ID);
		a.recycle();
	}

	@Override
	public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection) {
		boolean layoutChild = super.onLayoutChild(parent, child, layoutDirection);
		if (mTabLayout == null && mTabLayoutId != View.NO_ID) {
			mTabLayout = findTabLayout(child);
		}
		return layoutChild;
	}

	private TabLayout findTabLayout(View child) {
		if (mTabLayoutId == 0) return null;
		return (TabLayout) child.findViewById(mTabLayoutId);
	}

	@Override
	public boolean onDependentViewChanged(CoordinatorLayout parent, V child, View dependency) {
		return super.onDependentViewChanged(parent, child, dependency);
	}

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

	@Override
	public boolean layoutDependsOn(CoordinatorLayout parent, V child, View dependency) {
		updateSnackbar(child, dependency);
		updateFloatingActionButton(dependency);
		return super.layoutDependsOn(parent, child, dependency);
	}

	@Override
	public void onNestedVerticalOverScroll(CoordinatorLayout coordinatorLayout, V child, @ScrollDirection int direction, int currentOverScroll, int totalOverScroll) {
	}

	@Override
	public void onDirectionNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View target, int dx, int dy, int[] consumed, @ScrollDirection int scrollDirection) {
	}

	@Override
	protected boolean onNestedDirectionFling(CoordinatorLayout coordinatorLayout, V child, View target, float velocityX, float velocityY, @ScrollDirection int scrollDirection) {
		return false;
	}

	@Override
	public void onNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
		super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
		if (dyConsumed < 0) {
			handleDirection(child, ScrollDirection.SCROLL_DIRECTION_DOWN);
		} else if (dyConsumed > 0) {
			handleDirection(child, ScrollDirection.SCROLL_DIRECTION_UP);
		}
	}

	@Override
	public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, V child, View directTargetChild, View target, int nestedScrollAxes) {
		return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
	}

	/**
	 * Handle scroll direction
	 * @param child
	 * @param scrollDirection
	 */
	private void handleDirection(V child, int scrollDirection) {
		if (!behaviorTranslationEnabled) {
			return;
		}
		if (scrollDirection == ScrollDirection.SCROLL_DIRECTION_DOWN && hidden) {
			hidden = false;
			animateOffset(child, 0, false, true);
		} else if (scrollDirection == ScrollDirection.SCROLL_DIRECTION_UP && !hidden) {
			hidden = true;
			animateOffset(child, child.getHeight(), false, true);
		}
	}

	/**
	 * Animate offset
	 *
	 * @param child
	 * @param offset
	 */
	private void animateOffset(final V child, final int offset, boolean forceAnimation, boolean withAnimation) {
		if (!behaviorTranslationEnabled && !forceAnimation) {
			return;
		}
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
			ensureOrCancelObjectAnimation(child, offset, withAnimation);
			translationObjectAnimator.start();
		} else {
			ensureOrCancelAnimator(child, withAnimation);
			translationAnimator.translationY(offset).start();
		}
	}

	/**
	 * Manage animation for Android >= KITKAT
	 *
	 * @param child
	 */
	private void ensureOrCancelAnimator(V child, boolean withAnimation) {
		if (translationAnimator == null) {
			translationAnimator = ViewCompat.animate(child);
			translationAnimator.setDuration(withAnimation ? ANIM_DURATION : 0);
			translationAnimator.setUpdateListener(new ViewPropertyAnimatorUpdateListener() {
				@Override
				public void onAnimationUpdate(View view) {
					// Animate snackbar
					if (snackbarLayout != null && snackbarLayout.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
						targetOffset = view.getMeasuredHeight() - view.getTranslationY();
						ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) snackbarLayout.getLayoutParams();
						p.setMargins(p.leftMargin, p.topMargin, p.rightMargin, (int) targetOffset);
						snackbarLayout.requestLayout();
					}
					// Animate Floating Action Button
					if (floatingActionButton != null && floatingActionButton.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
						ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) floatingActionButton.getLayoutParams();
						fabTargetOffset = fabDefaultBottomMargin - view.getTranslationY() + snackBarY;
						p.setMargins(p.leftMargin, p.topMargin, p.rightMargin, (int) fabTargetOffset);
						floatingActionButton.requestLayout();
					}
				}
			});
			translationAnimator.setInterpolator(INTERPOLATOR);
		} else {
			translationAnimator.setDuration(withAnimation ? ANIM_DURATION : 0);
			translationAnimator.cancel();
		}
	}

	/**
	 * Manage animation for Android < KITKAT
	 *
	 * @param child
	 */
	private void ensureOrCancelObjectAnimation(final V child, final int offset, boolean withAnimation) {

		if (translationObjectAnimator != null) {
			translationObjectAnimator.cancel();
		}

		translationObjectAnimator = ObjectAnimator.ofFloat(child, View.TRANSLATION_Y, offset);
		translationObjectAnimator.setDuration(withAnimation ? ANIM_DURATION : 0);
		translationObjectAnimator.setInterpolator(INTERPOLATOR);
		translationObjectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				if (snackbarLayout != null && snackbarLayout.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
					targetOffset = child.getMeasuredHeight() - child.getTranslationY();
					ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) snackbarLayout.getLayoutParams();
					p.setMargins(p.leftMargin, p.topMargin, p.rightMargin, (int) targetOffset);
					snackbarLayout.requestLayout();
				}
				// Animate Floating Action Button
				if (floatingActionButton != null && floatingActionButton.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
					ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) floatingActionButton.getLayoutParams();
					fabTargetOffset = fabDefaultBottomMargin - child.getTranslationY() + snackBarY;
					p.setMargins(p.leftMargin, p.topMargin, p.rightMargin, (int) fabTargetOffset);
					floatingActionButton.requestLayout();
				}
			}
		});
	}


	public static <V extends View> AHBottomNavigationBehavior<V> from(V view) {
		ViewGroup.LayoutParams params = view.getLayoutParams();
		if (!(params instanceof CoordinatorLayout.LayoutParams)) {
			throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
		}
		CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) params)
				.getBehavior();
		if (!(behavior instanceof AHBottomNavigationBehavior)) {
			throw new IllegalArgumentException(
					"The view is not associated with AHBottomNavigationBehavior");
		}
		return (AHBottomNavigationBehavior<V>) behavior;
	}

	public void setTabLayoutId(int tabId) {
		this.mTabLayoutId = tabId;
	}

	/**
	 * Enable or not the behavior translation
	 * @param behaviorTranslationEnabled
	 */
	public void setBehaviorTranslationEnabled(boolean behaviorTranslationEnabled) {
		this.behaviorTranslationEnabled = behaviorTranslationEnabled;
	}

	/**
	 * Hide AHBottomNavigation with animation
	 * @param view
	 * @param offset
	 */
	public void hideView(V view, int offset, boolean withAnimation) {
		if (!hidden) {
			hidden = true;
			animateOffset(view, offset, true, withAnimation);
		}
	}

	/**
	 * Reset AHBottomNavigation position with animation
	 * @param view
	 */
	public void resetOffset(V view, boolean withAnimation) {
		if (hidden) {
			hidden = false;
			animateOffset(view, 0, true, withAnimation);
		}
	}

	/**
	 * Update Snackbar bottom margin
	 */
	public void updateSnackbar(final View child, View dependency) {

		if (dependency != null && dependency instanceof Snackbar.SnackbarLayout) {

			snackbarLayout = (Snackbar.SnackbarLayout) dependency;
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
				snackbarLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
					@Override
					public void onLayoutChange(View v, int left, int top, int right, int bottom,
					                           int oldLeft, int oldTop, int oldRight, int oldBottom) {
						if (floatingActionButton != null &&
								floatingActionButton.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
							ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) floatingActionButton.getLayoutParams();
							snackBarY = bottom - v.getY();
							fabTargetOffset = fabDefaultBottomMargin - child.getTranslationY() + snackBarY;
							p.setMargins(p.leftMargin, p.topMargin, p.rightMargin, (int) fabTargetOffset);
							floatingActionButton.requestLayout();
						}
					}
				});
			}

			if (mSnackbarHeight == -1) {
				mSnackbarHeight = dependency.getHeight();
			}

			int targetMargin = (int) (child.getMeasuredHeight() - child.getTranslationY());
			if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
				child.bringToFront();
			}

			if (dependency.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
				ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) dependency.getLayoutParams();
				p.setMargins(p.leftMargin, p.topMargin, p.rightMargin, targetMargin);
				dependency.requestLayout();
			}
		}
	}

	/**
	 * Update floating action button bottom margin
	 */
	public void updateFloatingActionButton(View dependency) {
		if (dependency != null && dependency instanceof FloatingActionButton) {
			floatingActionButton = (FloatingActionButton) dependency;
			if (!fabBottomMarginInitialized && dependency.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
				fabBottomMarginInitialized = true;
				ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) dependency.getLayoutParams();
				fabDefaultBottomMargin = p.bottomMargin;
			}
		}
	}
}