0

I have implemented a ViewPager and a FloatingActionButton within a CoordinatorLayout. The FloatingActionButton have a layout_behavior set to custom ScrollAwareFABBehavior. On swiping pager, the fab changes visibillity (animation) successfully and as well on scrolling nested views. I've implemented any necessary support libraries (23.3.0).

The pager shows 2 Fragments:

  1. RecyclerView (have to show fab)
  2. NestedScrollView (must not show fab)

On page changes, the activity asks child fragments (interface) whether to show fab or not. That works great, but on scrolling down the scroll view in 2nd fragment, the fab is visible again.

How to prevent showing fab again on scrolling down NestedScrollView?

Community
  • 1
  • 1
silversmurf
  • 435
  • 3
  • 10
  • you can add FloatingActionButton to the 1st fragment only instead of activity, or hide it manually on starting the 2nd fragment – arjunkn Aug 19 '16 at 10:47
  • if I add it on the first fragment, it slides away with viewpager slides. thats not what the guidelines says: The fab has to stay at the same position on sliding viewpager views! – silversmurf Aug 19 '16 at 10:56

1 Answers1

1

Ok, I got an Answer from an G+ User (credits to Christophe Beyls)!

Retrieving CoordinatorLayout.Behavior instance on main activity (or main fragment):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    CoordinatorLayout rootView = (CoordinatorLayout) inflater.inflate(R.layout.main, container, false);
    FloatingActionButton fab = (FloatingActionButton) mRootView.findViewById(R.id.fab);
    CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
    CoordinatorLayout.Behavior b = lp.getBehavior();

    if (b instanceof ScrollAwareFABBehavior) { // my custom behavior
        // saving ref for later use
        mFABBehavior = (ScrollAwareFABBehavior) b;
    }

    // do other stuff

    return rootView;
}

In ScrollAwareFABBehavior.java add:

public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
    private boolean mFabUserHidden = false;

    // other stuff in here

    /**
     * Overrides the default FAB show/hide functionality
     *
     * @param hidden    TRUE to stay hidden, false otherwise
     */
    public void stayHidden(boolean hidden) {
        mFabUserHidden = hidden;
    }

    @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 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) {
            animateOut(child);
        } else if (dyConsumed < 0 && !mFabUserHidden && child.getVisibility() != View.VISIBLE) {
            animateIn(child);
        }
    }

    // do other stuff

}

In ViewPager.OnPageChangeListener you can set for every fragment if the fab should be visible or not.

Maybe it will help anybody with the same problem!!

silversmurf
  • 435
  • 3
  • 10