OnNestedScroll - 有时不触发

时间:2016-08-03 17:01:09

标签: android android-layout android-coordinatorlayout android-nestedscrollview

当我在NestedScrollView内滚动CoordinatorLayout时,我正试图显示/隐藏FAB。我用Behavior实现了这一点,你可以在这个链接上查看github example)。问题是在OnNestedScroll方法中调用show / hide方法。不幸的是,当我滚动这个方法时不是然而,onNestedScrollAccepted方法总是被调用。我找不到任何理由为什么它不能打电话(比如20-30%的时间)。你知道为什么会这样吗? ?

这是我的行为类:

public class ScrollAwareFABBehavior  extends CoordinatorLayout.Behavior {
    FloatingActionMenu fabMenu;
    Context cont;

    public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
        super();
        cont =context;
    }
    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child,
                                       View directTargetChild, View target, int nestedScrollAxes) {
        if (nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL)
        {
            return true;
        }
        return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,
                        nestedScrollAxes);
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target,
                               int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        fabMenu = (FloatingActionMenu) child;
        Log.d("TAG","onNestedScroll"); // Sometimes it is not called
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
                dyUnconsumed);
        if (dyConsumed > 0 && !fabMenu.isMenuButtonHidden()) {
            fabMenu.hideMenuButton(true);
        } else if (dyConsumed < 0 && fabMenu.isMenuButtonHidden()) {
            fabMenu.showMenuButton(true);
        }
    }
    @Override
    public void onNestedScrollAccepted(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
        Log.d("TAG","Accepted"); //This is always called
       onStartNestedScroll( coordinatorLayout,  child,  directTargetChild,  target,  nestedScrollAxes);
    }

    @Override
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target) {
        Log.d("TAG","STOP");
        super.onStopNestedScroll(coordinatorLayout, child, target);
    }
    }

0 个答案:

没有答案