/*
* Copyright 2017 GcsSloop
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Last modified 2017-03-31 03:42:32
*
* GitHub: https://github.com/GcsSloop
* Website: http://www.gcssloop.com
* Weibo: http://weibo.com/GcsSloop
*/
package com.gcssloop.diycode.widget.behavior;
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;
// FAB 行为控制器
public class ScaleDownShowBehavior extends FloatingActionButton.Behavior {
public ScaleDownShowBehavior(Context context, AttributeSet attrs) {
super();
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
FloatingActionButton child, View directTargetChild,
View target, int nestedScrollAxes) {
if (nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL){
return true;
}
return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild,
target, nestedScrollAxes);
}
private boolean isAnimateIng = false; // 是否正在动画
private boolean isShow = true; // 是否已经显示
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child,
View target, int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed) {
if ((dyConsumed > 0 || dyUnconsumed > 0) && !isAnimateIng && isShow) {// 手指上滑,隐藏FAB
AnimatorUtil.translateHide(child, new StateListener() {
@Override
public void onAnimationStart(View view) {
super.onAnimationStart(view);
isShow = false;
}
});
} else if ((dyConsumed < 0 || dyUnconsumed < 0 && !isAnimateIng && !isShow)) {
AnimatorUtil.translateShow(child, new StateListener() {
@Override
public void onAnimationStart(View view) {
super.onAnimationStart(view);
isShow = true;
}
});// 手指下滑,显示FAB
}
}
class StateListener implements ViewPropertyAnimatorListener {
@Override
public void onAnimationStart(View view) {
isAnimateIng = true;
}
@Override
public void onAnimationEnd(View view) {
isAnimateIng = false;
}
@Override
public void onAnimationCancel(View view) {
isAnimateIng = false;
}
}
}