Created
June 4, 2016 15:35
-
-
Save Nathaniel100/4e5c557b86885803779a44b13407aaae to your computer and use it in GitHub Desktop.
FloatingActionButton dismisses when scroll down, and shows when scroll up
This file contains hidden or 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
/* | |
* Copyright 2015 The Android Open Source Project | |
* | |
* 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 com.support.android.designlibdemo; | |
import android.content.Context; | |
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; | |
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior { | |
public ScrollAwareFABBehavior(Context context, AttributeSet attrs) { | |
super(); | |
} | |
@Override | |
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, | |
final View directTargetChild, final View target, final int nestedScrollAxes) { | |
// Ensure we react to vertical scrolling | |
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL | |
|| super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes); | |
} | |
@Override | |
public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, | |
final View target, final int dxConsumed, final int dyConsumed, | |
final int dxUnconsumed, final int dyUnconsumed) { | |
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed); | |
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) { | |
// User scrolled down and the FAB is currently visible -> hide the FAB | |
child.hide(); | |
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) { | |
// User scrolled up and the FAB is currently not visible -> show the FAB | |
child.show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment