co.herdy.manager.presentation.view.behaviour.ScrollAwareFloatingActionButtonBehavior.java Source code

Java tutorial

Introduction

Here is the source code for co.herdy.manager.presentation.view.behaviour.ScrollAwareFloatingActionButtonBehavior.java

Source

/*
 * Copyright 2016 Oti Rowland
 *
 * 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.
 *
 */

package co.herdy.manager.presentation.view.behaviour;

import android.content.Context;
import android.os.Build;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import co.herdy.manager.R;

/**
 * Created by Oti Rowland on 1/1/2016.
 */
public class ScrollAwareFloatingActionButtonBehavior extends FloatingActionButton.Behavior {

    private static final boolean APPBARLAYOUT_BEHAVIOR_ENABLED = Build.VERSION.SDK_INT >= 11;

    // Default constructor
    public ScrollAwareFloatingActionButtonBehavior(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);
    }

    //
    @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);

        if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
            child.hide();
        } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
            child.show();
            // Create a Simple growth Animation
            Animation simpleGrowAnimation = AnimationUtils.loadAnimation(child.getContext(), R.anim.grow_bigger);
            child.startAnimation(simpleGrowAnimation);
        }
    }

    // We depend on the AppBarLayout
    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
        // We're dependent on all AppbarLayouts (if enabled)
        return APPBARLAYOUT_BEHAVIOR_ENABLED && dependency instanceof AppBarLayout;
    }
}