美文网首页
BottomNavigationBehavior上滑下拉隐藏显示

BottomNavigationBehavior上滑下拉隐藏显示

作者: 玖玖君 | 来源:发表于2019-07-31 20:57 被阅读0次
    public class BottomNavigationBehavior extends CoordinatorLayout.Behavior<View> {
    
        private ObjectAnimator outAnimator, inAnimator;
    
        public BottomNavigationBehavior(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        // 垂直滑动
        @Override
        public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
            return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
        }
    
        @Override
        public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
            if (dy > 0) {// 上滑隐藏
                if (outAnimator == null) {
                    outAnimator = ObjectAnimator.ofFloat(child, "translationY", 0, child.getHeight());
                    outAnimator.setDuration(200);
                }
                if (!outAnimator.isRunning() && child.getTranslationY() <= 0) {
                    outAnimator.start();
                }
            } else if (dy < 0) {// 下滑显示
                if (inAnimator == null) {
                    inAnimator = ObjectAnimator.ofFloat(child, "translationY", child.getHeight(), 0);
                    inAnimator.setDuration(200);
                }
                if (!inAnimator.isRunning() && child.getTranslationY() >= child.getHeight()) {
                    inAnimator.start();
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:BottomNavigationBehavior上滑下拉隐藏显示

          本文链接:https://www.haomeiwen.com/subject/omsxdctx.html