Created
September 13, 2015 09:15
-
-
Save keng42/e45de43e98f215837d0f to your computer and use it in GitHub Desktop.
FAB 根据列表的滚动而向上和向下移动
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.keng42.jkisms.util; | |
import android.content.Context; | |
import android.support.design.widget.CoordinatorLayout; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.v4.view.ViewCompat; | |
import android.support.v4.view.ViewPropertyAnimatorListener; | |
import android.util.AttributeSet; | |
import android.view.View; | |
/** | |
* Created by Keng on 2015/9/13 | |
* FAB 根据列表的滚动而向上和向下移动 | |
*/ | |
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior { | |
public ScrollAwareFABBehavior(Context context, AttributeSet attrs) { | |
super(); | |
} | |
@Override | |
public boolean onStartNestedScroll( | |
CoordinatorLayout coordinatorLayout, FloatingActionButton child, | |
View directTargetChild, View target, int nestedScrollAxes) { | |
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || | |
super.onStartNestedScroll(coordinatorLayout, child, | |
directTargetChild, target, nestedScrollAxes); | |
} | |
// 收尾动画是否执行中 | |
private boolean mAnimating; | |
// 按钮是否正在向上移动 | |
private boolean mFabMovingUp; | |
@Override | |
public void onStopNestedScroll( | |
CoordinatorLayout coordinatorLayout, | |
FloatingActionButton child, View target) { | |
super.onStopNestedScroll(coordinatorLayout, child, target); | |
// 当前动画结束后才能进行下一次动画 | |
ViewPropertyAnimatorListener listener = new ViewPropertyAnimatorListener() { | |
@Override | |
public void onAnimationStart(View view) { | |
mAnimating = true; | |
} | |
@Override | |
public void onAnimationEnd(View view) { | |
mAnimating = false; | |
} | |
@Override | |
public void onAnimationCancel(View view) { | |
mAnimating = false; | |
} | |
}; | |
if (mFabMovingUp && !mAnimating) { | |
// 按钮之前做上移动作,完成剩余移动 | |
ViewCompat.animate(child).translationY(0).withLayer().setListener(listener).start(); | |
} | |
if (!mFabMovingUp && !mAnimating) { | |
// 按钮之前做下移动作,完成剩余移动 | |
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams(); | |
int fabBottomMargin = lp.bottomMargin; | |
int distanceToScroll = child.getHeight() + fabBottomMargin; | |
ViewCompat.animate(child).translationY(distanceToScroll).withLayer().setListener(listener).start(); | |
} | |
} | |
@Override | |
public void onNestedScroll( | |
CoordinatorLayout coordinatorLayout, FloatingActionButton child, | |
View target, int dxConsumed, int dyConsumed, int dxUnconsumed, | |
int dyUnconsumed) { | |
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, | |
dyConsumed, dxUnconsumed, dyUnconsumed); | |
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams(); | |
int fabBottomMargin = lp.bottomMargin; | |
int distanceToScroll = child.getHeight() + fabBottomMargin; | |
if (dyConsumed > 0) { | |
// 手指向上划,按钮向下移动 | |
mFabMovingUp = false; | |
if (child.getTranslationY() <= distanceToScroll) { | |
child.setTranslationY(child.getTranslationY() + dyConsumed); | |
} | |
} else if (dyConsumed < 0) { | |
mFabMovingUp = true; | |
if (child.getTranslationY() >= 0) { | |
child.setTranslationY(child.getTranslationY() + dyConsumed); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment