【问题标题】:Hiding BottomNavigationView on scroll在滚动时隐藏 BottomNavigationView
【发布时间】:2025-12-16 03:45:02
【问题描述】:
【问题讨论】:
标签:
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;
}