BottomNavigationView 与 RecyclerView 垂直滚动的垂直滚动 CoordinatorLayout 行为
Vertical Scrolling CoordinatorLayout Behavior for BottomNavigationView with RecyclerView vertical scrolling
如何定义与 RecyclerView 垂直滚动同步的 CoordinatorLayout.Behavior class for BottomNavigationView 滚动。
我看过 and ,但它所做的只是立即 show/hide 和 NavigationView
垂直滚动事件。我不想立即 show/hide NavigationView
,而是我想要一种类似于 AppbarLayout
的行为,它有一个 Toolbar
,滚动标志为 app:layout_scrollFlags="scroll|enterAlways"
。
public class BottomNavigationBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {
public BottomNavigationBehavior() {
super();
}
public BottomNavigationBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, BottomNavigationView child, View dependency) {
boolean dependsOn = dependency instanceof FrameLayout;
return dependsOn;
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View target, int dx, int dy, int[] consumed) {
if(dy < 0) {
showBottomNavigationView(child);
}
else if(dy > 0) {
hideBottomNavigationView(child);
}
}
private void hideBottomNavigationView(BottomNavigationView view) {
view.animate().translationY(view.getHeight());
}
private void showBottomNavigationView(BottomNavigationView view) {
view.animate().translationY(0);
}
}
经过一番尝试,我想到了这个解决方案:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(dy > 0 && visible){
mBinding.bnv.test.setY(mBinding.bnv.getY() + dy);
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
visible = mBiding.bnv.getY() > metrics.heightPixels;
if(!visible) {
mBinding.bnv.setY(metrics.heightPixels);
}
} else {
mBinding.bnv.setY(mBinding.bnv.getY() + dy);
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
visible = mBinding.bnv.getY() > metrics.heightPixels;
}
}
因此您正在使用回收器视图滚动 BottomNavigationView
或 CoordinatorLayout.Behavior class:
public class ViewScrollWithRecyclerViewBehavior extends CoordinatorLayout.Behavior<View> {
private boolean visible = true;
private boolean inStartPosition = true;
private float oldY;
private DisplayMetrics metrics;
public ViewScrollWithRecyclerViewBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
metrics = Resources.getSystem().getDisplayMetrics();
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View fab, View dependency) {
return dependency instanceof AppBarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
if (dependency instanceof AppBarLayout) {
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
float dy = oldY - dependency.getY();
if(dy > 0 && visible){
moveDown(child, oldY);
} else if(!inStartPosition) {
moveUp(child, oldY);
}
oldY = dependency.getY();
}
return true;
}
private void moveUp(View child, float dy){
if(child.getY() + dy >= metrics.heightPixels - child.getHeight()){
child.setY(metrics.heightPixels - child.getHeight());
} else {
child.setY(child.getY() + dy);
}
inStartPosition = child.getY() == metrics.heightPixels - child.getHeight();
visible = child.getY() > metrics.heightPixels;
}
private void moveDown(View child, float dy){
child.setY(child.getY() + dy);
visible = child.getY() > metrics.heightPixels;
if(!visible) {
child.setY(metrics.heightPixels);
}
}
@Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final View child,
final View directTargetChild, final View target, final int nestedScrollAxes) {
return true;
}
@Override
public void onNestedScroll(final CoordinatorLayout coordinatorLayout,
final View child,
final View target, final int dxConsumed, final int dy,
final int dxUnconsumed, final int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dy, dxUnconsumed, dyUnconsumed);
if(dy > 0 && visible){
moveDown(child, dy);
} else if(!inStartPosition) {
moveUp(child, dy);
}
}
}
如何定义与 RecyclerView 垂直滚动同步的 CoordinatorLayout.Behavior class for BottomNavigationView 滚动。
我看过 NavigationView
垂直滚动事件。我不想立即 show/hide NavigationView
,而是我想要一种类似于 AppbarLayout
的行为,它有一个 Toolbar
,滚动标志为 app:layout_scrollFlags="scroll|enterAlways"
。
public class BottomNavigationBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {
public BottomNavigationBehavior() {
super();
}
public BottomNavigationBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, BottomNavigationView child, View dependency) {
boolean dependsOn = dependency instanceof FrameLayout;
return dependsOn;
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View target, int dx, int dy, int[] consumed) {
if(dy < 0) {
showBottomNavigationView(child);
}
else if(dy > 0) {
hideBottomNavigationView(child);
}
}
private void hideBottomNavigationView(BottomNavigationView view) {
view.animate().translationY(view.getHeight());
}
private void showBottomNavigationView(BottomNavigationView view) {
view.animate().translationY(0);
}
}
经过一番尝试,我想到了这个解决方案:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(dy > 0 && visible){
mBinding.bnv.test.setY(mBinding.bnv.getY() + dy);
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
visible = mBiding.bnv.getY() > metrics.heightPixels;
if(!visible) {
mBinding.bnv.setY(metrics.heightPixels);
}
} else {
mBinding.bnv.setY(mBinding.bnv.getY() + dy);
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
visible = mBinding.bnv.getY() > metrics.heightPixels;
}
}
因此您正在使用回收器视图滚动 BottomNavigationView
或 CoordinatorLayout.Behavior class:
public class ViewScrollWithRecyclerViewBehavior extends CoordinatorLayout.Behavior<View> {
private boolean visible = true;
private boolean inStartPosition = true;
private float oldY;
private DisplayMetrics metrics;
public ViewScrollWithRecyclerViewBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
metrics = Resources.getSystem().getDisplayMetrics();
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View fab, View dependency) {
return dependency instanceof AppBarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
if (dependency instanceof AppBarLayout) {
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
float dy = oldY - dependency.getY();
if(dy > 0 && visible){
moveDown(child, oldY);
} else if(!inStartPosition) {
moveUp(child, oldY);
}
oldY = dependency.getY();
}
return true;
}
private void moveUp(View child, float dy){
if(child.getY() + dy >= metrics.heightPixels - child.getHeight()){
child.setY(metrics.heightPixels - child.getHeight());
} else {
child.setY(child.getY() + dy);
}
inStartPosition = child.getY() == metrics.heightPixels - child.getHeight();
visible = child.getY() > metrics.heightPixels;
}
private void moveDown(View child, float dy){
child.setY(child.getY() + dy);
visible = child.getY() > metrics.heightPixels;
if(!visible) {
child.setY(metrics.heightPixels);
}
}
@Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final View child,
final View directTargetChild, final View target, final int nestedScrollAxes) {
return true;
}
@Override
public void onNestedScroll(final CoordinatorLayout coordinatorLayout,
final View child,
final View target, final int dxConsumed, final int dy,
final int dxUnconsumed, final int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dy, dxUnconsumed, dyUnconsumed);
if(dy > 0 && visible){
moveDown(child, dy);
} else if(!inStartPosition) {
moveUp(child, dy);
}
}
}