好的,我得到了 G+ 用户的答复(Christophe Beyls 的学分)!
在主要活动(或主要片段)上检索CoordinatorLayout.Behavior实例:
@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;
}
在ScrollAwareFABBehavior.java添加:
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
}
如果ViewPager.OnPageChangeListener
工厂应该可见或不可见,您可以为每个片段设置。
也许它会帮助任何有同样问题的人!!