Hiding BottomNavigationView on scroll

自闭症网瘾萝莉.ら 提交于 2019-12-11 05:15:33

问题


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


来源:https://stackoverflow.com/questions/43142441/hiding-bottomnavigationview-on-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!