当我们使用CoordinatorLayout中的 BottomSheetBehavior 抽屉式布局时, 快速滑动RecyclerView或者NestedScrollView 时,会触发BottomSheetBehavior 的STATE_COLLAPSED 动作,导致抽屉布局被异常关闭,效果很差。
解决:
查看BottomSheetBehavior 源码发现,使抽屉布局关闭的是如下代码
@Override
public void onNestedPreScroll(
@NonNull CoordinatorLayout coordinatorLayout,
@NonNull V child,
@NonNull View target,
int dx,
int dy,
@NonNull int[] consumed,
int type) {
if (type == ViewCompat.TYPE_NON_TOUCH) {
// Ignore fling here. The ViewDragHelper handles it.
return;
}
View scrollingChild = nestedScrollingChildRef !=
提升BottomSheetBehavior可控性:解决RecyclerView/NestedScrollView滑动导致抽屉关闭问题

本文探讨了在使用CoordinatorLayout的BottomSheetBehavior时,如何避免RecyclerView或NestedScrollView的快速滑动意外关闭抽屉。通过理解源码,作者揭示了解决方案,即在适当条件下阻止非拖动行为,增强抽屉的可控性。
最低0.47元/天 解锁文章
9324





