防止FloatingActionButton在CoordinatorLayout,ViewPager,Fragment中变得可见

时间:2016-08-19 09:58:49

标签: android android-viewpager android-coordinatorlayout floating-action-button android-nestedscrollview

我在ViewPager内实施了FloatingActionButtonCoordinatorLayoutFloatingActionButtonlayout_behavior设置为自定义ScrollAwareFABBehavior。在刷页面时,工厂成功地改变了visibillity(动画)以及滚动嵌套视图。我已经实施了任何必要的支持库(23.3.0)。

寻呼机显示2 Fragments

  1. RecyclerView(必须展示工厂)
  2. NestedScrollView(不得显示工厂)
  3. 在页面更改时,活动会询问子片段(界面)是否显示fab。这很有效,但是在向下滚动第二个片段中的滚动视图时,fab再次可见。

    如何防止在向下滚动NestedScrollView时显示fab?

1 个答案:

答案 0 :(得分:1)

好的,我从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中,如果工厂应该可见,您可以为每个片段设置。

也许它会帮助任何有同样问题的人!!