Hiding BottomNavigationView on scroll

2019-07-25 16:39发布

问题:

I am implementing the bottom navigation bar of the material design https://material.io/guidelines/components/bottom-navigation.html

It suggests that while scrolling down , we should hide the bar , and show it while scrolling up.

I am a little lost in how to go about this. Should I have to manually do that , or there is some functionality built in inside the view that would do it.

Do I have some behaviour for this ? (as the bottomnavigation is a child of coord layout)

回答1:

This is work for me.

I have used "slide up" and "slide down" animation for my gridview. when grid scroll upward then hide bottomNavBar and when scroll downward then show bottomNavBar. that's it.

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;
}