1

I'm using a FloatingActionButton in a CoordinatorLayout but unlike the normal behaviour I want to show it when the bottom is reached in order to allow the user to see more data.

To do so, I found on the Internet this Behavior for the component.

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;
}

Unfortunately, I couldn't figure out how to do it. I have been playing with fab.setTranslationY() without success.

Anyone can show me any way to do it?

EDIT: I'm using mzgreen example: https://github.com/mzgreen/HideOnScrollExample

kike
  • 4,255
  • 5
  • 23
  • 41

1 Answers1

1

I had the same problem using makovastar example (https://github.com/makovkastar/FloatingActionButton). This answer pointed me to the solution.

There is a inner class inside FloatingActionButton.java (FloatingActionButton#AbsListViewScrollDetectorImpl) that implements the scrolling methods.

Just add the following code to onScroll method:

if (view.getLastVisiblePosition() == view.getAdapter().getCount() -1 &&
                    view.getChildAt(view.getChildCount() - 1).getBottom() <= view.getHeight()){
                //It is scrolled all the way down here so we show the button
                show();
            }
            else hide();

I hope it works!

Community
  • 1
  • 1
mallorente
  • 78
  • 4