Reputation: 485
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
:
RecyclerView
(have to show fab)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?
Upvotes: 0
Views: 620
Reputation: 485
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!!
Upvotes: 1