mirror of https://github.com/ThmmyNoLife/mTHMMY
Apostolos Fanakis
8 years ago
14 changed files with 156 additions and 69 deletions
@ -0,0 +1,122 @@ |
|||
package gr.thmmy.mthmmy.utils; |
|||
|
|||
import android.animation.Animator; |
|||
import android.content.Context; |
|||
import android.support.design.widget.CoordinatorLayout; |
|||
import android.support.v4.view.ViewCompat; |
|||
import android.support.v4.view.animation.FastOutSlowInInterpolator; |
|||
import android.util.AttributeSet; |
|||
import android.view.View; |
|||
import android.view.ViewPropertyAnimator; |
|||
|
|||
/** |
|||
* CoordinatorLayout Behavior for bottom navigation bar. |
|||
* <p>When a nested ScrollView is scrolled down, the view will disappear. |
|||
* When the ScrollView is scrolled back up, the view will reappear.</p> |
|||
*/ |
|||
public class ScrollAwareLinearBehavior extends CoordinatorLayout.Behavior<View> { |
|||
private int mDySinceDirectionChange; |
|||
private boolean mIsShowing; |
|||
private boolean mIsHiding; |
|||
|
|||
public ScrollAwareLinearBehavior(Context context, AttributeSet attrs) { |
|||
super(context, attrs); |
|||
} |
|||
|
|||
@Override |
|||
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) { |
|||
return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0; |
|||
} |
|||
|
|||
@Override |
|||
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) { |
|||
if (dy > 0 && mDySinceDirectionChange < 0 |
|||
|| dy < 0 && mDySinceDirectionChange > 0) { |
|||
child.animate().cancel(); |
|||
mDySinceDirectionChange = 0; |
|||
} |
|||
|
|||
mDySinceDirectionChange += dy; |
|||
|
|||
if (mDySinceDirectionChange > child.getHeight() |
|||
&& child.getVisibility() == View.VISIBLE |
|||
&& !mIsHiding) { |
|||
hide(child); |
|||
} else if (mDySinceDirectionChange < 0 |
|||
&& child.getVisibility() == View.INVISIBLE |
|||
&& !mIsShowing) { |
|||
show(child); |
|||
} |
|||
} |
|||
|
|||
private void hide(final View view) { |
|||
mIsHiding = true; |
|||
ViewPropertyAnimator animator = view.animate() |
|||
.translationY(view.getHeight()) |
|||
.setInterpolator(new FastOutSlowInInterpolator()) |
|||
.setDuration(100); |
|||
|
|||
animator.setListener(new Animator.AnimatorListener() { |
|||
@Override |
|||
public void onAnimationStart(Animator animator) { |
|||
} |
|||
|
|||
@Override |
|||
public void onAnimationEnd(Animator animator) { |
|||
// Prevent drawing the View after it is gone
|
|||
mIsHiding = false; |
|||
view.setVisibility(View.INVISIBLE); |
|||
} |
|||
|
|||
@Override |
|||
public void onAnimationCancel(Animator animator) { |
|||
// Canceling a hide should show the view
|
|||
mIsHiding = false; |
|||
if (!mIsShowing) { |
|||
show(view); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onAnimationRepeat(Animator animator) { |
|||
} |
|||
}); |
|||
|
|||
animator.start(); |
|||
} |
|||
|
|||
private void show(final View view) { |
|||
mIsShowing = true; |
|||
ViewPropertyAnimator animator = view.animate() |
|||
.translationY(0) |
|||
.setInterpolator(new FastOutSlowInInterpolator()) |
|||
.setDuration(100); |
|||
|
|||
animator.setListener(new Animator.AnimatorListener() { |
|||
@Override |
|||
public void onAnimationStart(Animator animator) { |
|||
view.setVisibility(View.VISIBLE); |
|||
} |
|||
|
|||
@Override |
|||
public void onAnimationEnd(Animator animator) { |
|||
mIsShowing = false; |
|||
} |
|||
|
|||
@Override |
|||
public void onAnimationCancel(Animator animator) { |
|||
// Canceling a show should hide the view
|
|||
mIsShowing = false; |
|||
if (!mIsHiding) { |
|||
hide(view); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onAnimationRepeat(Animator animator) { |
|||
} |
|||
}); |
|||
|
|||
animator.start(); |
|||
} |
|||
} |
Before Width: | Height: | Size: 3.1 MiB |
After Width: | Height: | Size: 138 KiB |
Loading…
Reference in new issue