|
| 1 | +package com.zulip.android.util; |
| 2 | + |
| 3 | +import android.animation.Animator; |
| 4 | +import android.annotation.SuppressLint; |
| 5 | +import android.content.Context; |
| 6 | +import android.support.design.widget.AppBarLayout; |
| 7 | +import android.support.design.widget.CoordinatorLayout; |
| 8 | +import android.support.design.widget.FloatingActionButton; |
| 9 | +import android.support.design.widget.Snackbar; |
| 10 | +import android.support.v4.view.ViewCompat; |
| 11 | +import android.support.v4.view.animation.FastOutSlowInInterpolator; |
| 12 | +import android.util.AttributeSet; |
| 13 | +import android.util.TypedValue; |
| 14 | +import android.view.View; |
| 15 | +import android.view.ViewPropertyAnimator; |
| 16 | +import android.view.animation.Interpolator; |
| 17 | + |
| 18 | +import com.zulip.android.R; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +/** |
| 23 | + * This hides the {@link AppBarLayout} and {@link android.support.design.widget.FloatingActionButton} when the |
| 24 | + * recyclerView is scrolled, used in here {@link com.zulip.android.R.layout#main} as a behaviour. |
| 25 | + */ |
| 26 | +public class RemoveFabOnScroll extends CoordinatorLayout.Behavior<FloatingActionButton> { |
| 27 | + private static final Interpolator FAST_OUT_SLOW_IN_INTERPOLATOR = new FastOutSlowInInterpolator(); |
| 28 | + private static float toolbarHeight; |
| 29 | + private int changeInYDir; |
| 30 | + private boolean mIsShowing; |
| 31 | + private boolean isViewHidden; |
| 32 | + private View chatBox; |
| 33 | + |
| 34 | + public RemoveFabOnScroll(Context context, AttributeSet attrs) { |
| 35 | + super(context, attrs); |
| 36 | + TypedValue tv = new TypedValue(); |
| 37 | + if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) |
| 38 | + toolbarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics()); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) { |
| 43 | + return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0; |
| 44 | + } |
| 45 | + |
| 46 | + @SuppressLint("NewApi") |
| 47 | + @Override |
| 48 | + public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dx, int dy, int[] consumed) { |
| 49 | + if (dy > 0 && changeInYDir < 0 || dy < 0 && changeInYDir > 0) { |
| 50 | + child.animate().cancel(); |
| 51 | + changeInYDir = 0; |
| 52 | + } |
| 53 | + |
| 54 | + changeInYDir += dy; |
| 55 | + if (changeInYDir > toolbarHeight && child.getVisibility() == View.VISIBLE && !isViewHidden) |
| 56 | + hideView(child); |
| 57 | + else if (changeInYDir < 0 && child.getVisibility() == View.GONE && !mIsShowing) { |
| 58 | + if (child instanceof FloatingActionButton) { |
| 59 | + if (chatBox == null) |
| 60 | + chatBox = coordinatorLayout.findViewById(R.id.messageBoxContainer); |
| 61 | + if (chatBox.getVisibility() == View.VISIBLE) { |
| 62 | + return; |
| 63 | + } |
| 64 | + } |
| 65 | + showView(child); |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + @SuppressLint("NewApi") |
| 71 | + private void hideView(final View view) { |
| 72 | + isViewHidden = true; |
| 73 | + ViewPropertyAnimator animator = view.animate() |
| 74 | + .translationY((view instanceof AppBarLayout) ? -1 * view.getHeight() : view.getHeight()) |
| 75 | + .setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR) |
| 76 | + .setDuration(200); |
| 77 | + |
| 78 | + animator.setListener(new Animator.AnimatorListener() { |
| 79 | + @Override |
| 80 | + public void onAnimationStart(Animator animator) { |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onAnimationEnd(Animator animator) { |
| 85 | + isViewHidden = false; |
| 86 | + view.setVisibility(View.GONE); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void onAnimationCancel(Animator animator) { |
| 91 | + isViewHidden = false; |
| 92 | + if (!mIsShowing) |
| 93 | + showView(view); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void onAnimationRepeat(Animator animator) { |
| 98 | + } |
| 99 | + }); |
| 100 | + animator.start(); |
| 101 | + } |
| 102 | + |
| 103 | + @SuppressLint("NewApi") |
| 104 | + private void showView(final View view) { |
| 105 | + mIsShowing = true; |
| 106 | + ViewPropertyAnimator animator = view.animate() |
| 107 | + .translationY(0) |
| 108 | + .setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR) |
| 109 | + .setDuration(200); |
| 110 | + |
| 111 | + animator.setListener(new Animator.AnimatorListener() { |
| 112 | + @Override |
| 113 | + public void onAnimationStart(Animator animator) { |
| 114 | + view.setVisibility(View.VISIBLE); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void onAnimationEnd(Animator animator) { |
| 119 | + mIsShowing = false; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void onAnimationCancel(Animator animator) { |
| 124 | + mIsShowing = false; |
| 125 | + if (!isViewHidden) |
| 126 | + hideView(view); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void onAnimationRepeat(Animator animator) { |
| 131 | + } |
| 132 | + }); |
| 133 | + animator.start(); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) { |
| 138 | + return dependency instanceof Snackbar.SnackbarLayout; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) { |
| 143 | + float translationY = getFabTranslationYForSnackbar(parent, child); |
| 144 | + float percentComplete = -translationY / dependency.getHeight(); |
| 145 | + float scaleFactor = 1 - percentComplete; |
| 146 | + |
| 147 | + child.setScaleX(scaleFactor); |
| 148 | + child.setScaleY(scaleFactor); |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + private float getFabTranslationYForSnackbar(CoordinatorLayout parent, |
| 153 | + FloatingActionButton fab) { |
| 154 | + float minOffset = 0; |
| 155 | + final List<View> dependencies = parent.getDependencies(fab); |
| 156 | + for (int i = 0, z = dependencies.size(); i < z; i++) { |
| 157 | + final View view = dependencies.get(i); |
| 158 | + if (view instanceof Snackbar.SnackbarLayout && parent.doViewsOverlap(fab, view)) { |
| 159 | + minOffset = Math.min(minOffset, |
| 160 | + ViewCompat.getTranslationY(view) - view.getHeight()); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return minOffset; |
| 165 | + } |
| 166 | +} |
0 commit comments