0

我已经在 a 中实现了 aViewPagerFloatingActionButtona CoordinatorLayout。有FloatingActionButtonlayout_behavior组自定义ScrollAwareFABBehavior。在滑动寻呼机时,工厂成功更改可见性(动画)以及滚动嵌套视图。我已经实现了任何必要的支持库(23.3.0)。

寻呼机显示 2 Fragments

  1. RecyclerView(必须展示工厂)
  2. NestedScrollView(不得展示晶圆厂)

在页面更改时,活动会询问子片段(界面)是否显示 fab。这很好用,但是在第二个片段中向下滚动滚动视图时,工厂再次可见。

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

4

1 回答 1

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工厂应该可见或不可见,您可以为每个片段设置。

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

于 2016-08-19T13:33:16.020 回答