private void forbidAppBarScroll(boolean forbid) {
if (forbid == forbidAppBarScroll) {
return;
}
if (forbid) {
forbidAppBarScroll = true;
if (ViewCompat.isLaidOut(appBar)) {
setAppBarDragCallback(new AppBarLayout.Behavior.DragCallback() {
@Override public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
return false;
}
});
} else {
appBar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
appBar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
appBar.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
setAppBarDragCallback(new AppBarLayout.Behavior.DragCallback() {
@Override public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
return false;
}
});
}
});
}
} else {
forbidAppBarScroll = false;
if (ViewCompat.isLaidOut(appBar)) {
setAppBarDragCallback(null);
} else {
appBar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
appBar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
appBar.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
setAppBarDragCallback(null);
}
});
}
}
}
private void setAppBarDragCallback(AppBarLayout.Behavior.DragCallback dragCallback) {
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
behavior.setDragCallback(dragCallback);
}
禁止AppBarLayout滑动
最新推荐文章于 2023-11-17 17:53:20 发布