当用户到达底部时如何显示FloatingActionButton?

时间:2015-08-25 12:25:50

标签: android components material-design

我在CoordinatorLayout中使用FloatingActionButton,但与正常行为不同,我想在到达底部时显示它,以便让用户看到更多数据。

为此,我在互联网上找到了该组件的行为。

public class ScrollingFABBehavior extends FloatingActionButton.Behavior {
private int toolbarHeight;

public ScrollingFABBehavior(Context context, AttributeSet attrs) {
    super();
    this.toolbarHeight = Utils.getToolbarHeight(context);
}

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
    return super.layoutDependsOn(parent, fab, dependency) || (dependency instanceof AppBarLayout);
}

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
    boolean returnValue = super.onDependentViewChanged(parent, fab, dependency);
    if (dependency instanceof AppBarLayout) {
            CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
            int fabBottomMargin = lp.bottomMargin;
            int distanceToScroll = fab.getHeight() + fabBottomMargin;
            float ratio = (float)dependency.getY()/(float)toolbarHeight;
            fab.setTranslationY(-distanceToScroll * ratio);
    }
    return returnValue;
}

不幸的是,我无法弄明白该怎么做。我一直在玩fab.setTranslationY()但没有成功。

任何人都可以告诉我任何方法吗?

编辑:我正在使用mzgreen示例:https://github.com/mzgreen/HideOnScrollExample

0 个答案:

没有答案