6

I am trying to show/hide FAB when I scroll in NestedScrollView which is inside of CoordinatorLayout. I achieved that with Behavior which you can check on this link (github example). The problem is that is call show/hide method inside OnNestedScroll method. Unfortunately sometimes when I scroll this method is not called. However, onNestedScrollAcceptedmethod is called always. I don't find any reason why it isn't call (like 20-30% of time). Do you have any idea why does it happen?

This is my behavior class:

public class ScrollAwareFABBehavior  extends CoordinatorLayout.Behavior {
    FloatingActionMenu fabMenu;
    Context cont;

    public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
        super();
        cont =context;
    }
    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child,
                                       View directTargetChild, View target, int nestedScrollAxes) {
        if (nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL)
        {
            return true;
        }
        return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,
                        nestedScrollAxes);
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target,
                               int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        fabMenu = (FloatingActionMenu) child;
        Log.d("TAG","onNestedScroll"); // Sometimes it is not called
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
                dyUnconsumed);
        if (dyConsumed > 0 && !fabMenu.isMenuButtonHidden()) {
            fabMenu.hideMenuButton(true);
        } else if (dyConsumed < 0 && fabMenu.isMenuButtonHidden()) {
            fabMenu.showMenuButton(true);
        }
    }
    @Override
    public void onNestedScrollAccepted(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
        Log.d("TAG","Accepted"); //This is always called
       onStartNestedScroll( coordinatorLayout,  child,  directTargetChild,  target,  nestedScrollAxes);
    }

    @Override
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target) {
        Log.d("TAG","STOP");
        super.onStopNestedScroll(coordinatorLayout, child, target);
    }
    }
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
Tom Wayne
  • 1,288
  • 1
  • 12
  • 31
  • Hi. I have the same problem. Have you found a solution? – Vasily Kabunov Oct 26 '16 at 10:43
  • 1
    Hi. I am sorry I missed your question. The problem was that it wasn't called when was collapsing toolbar collapsing or expanding. I had to create custom listener which was triggered when collapsing layout changed its state. When it was triggerd I called same hide/ျshow method. This way It works in 100%. – Tom Wayne Nov 07 '16 at 13:01

0 Answers0