【问题标题】:Hiding BottomNavigationView on scroll在滚动时隐藏 BottomNavigationView
【发布时间】:2025-12-16 03:45:02
【问题描述】:

我正在实现材料设计的底部导航栏 https://material.io/guidelines/components/bottom-navigation.html

建议向下滚动时隐藏栏,向上滚动时显示。

我对如何去做这件事有点迷茫。我是否必须手动执行此操作,或者视图中内置了一些功能可以执行此操作。

我对此有什么行为吗? (因为底部导航是坐标布局的孩子)

【问题讨论】:

标签: android material-design android-coordinatorlayout


【解决方案1】:

这对我有用。

我为我的网格视图使用了“向上滑动”和“向下滑动”动画。当网格向上滚动时隐藏底部导航栏,向下滚动时显示底部导航栏。而已。

myGridView.setOnTouchListener(this);

int y, initialY, scrollingY, scrolledY;
boolean isVisible = true;

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {

    y = (int) motionEvent.getRawY();

    switch (motionEvent.getAction()) {

        case MotionEvent.ACTION_DOWN:
            initialY = (int) motionEvent.getRawY();
            Log.e("Down===", initialY+"");
            break;

        case MotionEvent.ACTION_MOVE:
            scrollingY = (int) motionEvent.getRawY();
            Log.e("Move===", scrollingY+"");

            switch (view.getId()) {

                case R.id.exploreGridMain:
                    if(isVisible && initialY > scrolledY) {
                        bottomNavigationView.startAnimation(slideDown);
                        slideDown.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                bottomNavigationView.setVisibility(View.GONE);
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });
                        isVisible = false;
                    } else if(!isVisible && initialY < scrolledY){
                        bottomNavigationView.startAnimation(slideUp);
                        slideUp.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                bottomNavigationView.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });
                        isVisible = true;
                    }
                    break;
            }
            scrolledY = scrollingY;
            break;

        case MotionEvent.ACTION_UP:
            Log.e("Up===", scrolledY+"-"+y);

            break;
    }
    return false;
}

【讨论】: