private void initBottomSheet() { mBottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.design_bottom_sheet)); // 默认设置为隐藏 mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); mBottomSheetBehavior.setBottomSheetCallback( new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, int newState) { Log.i( TAG, "onStateChanged bottomSheet " + bottomSheet.getClass().getSimpleName() + " newState " + newState); } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { Log.i( TAG, "onSlide bottomSheet " + bottomSheet.getClass().getSimpleName() + " newState " + slideOffset); } }); // 设置为 true 之后,点击或拖动时会被直接关闭 // mBottomSheetBehavior.setSkipCollapsed(true); }
private void initBottomView() { CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.cl_activity_scroll); assert coordinatorLayout != null; View view = coordinatorLayout.findViewById(R.id.ns_content_scroll); behavior = BottomSheetBehavior.from(view); behavior.setBottomSheetCallback( new BottomSheetBehavior.BottomSheetCallback() { boolean first = true; @Override public void onStateChanged(@NonNull View bottomSheet, int newState) { state = newState; } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { if (first) { first = false; bottomSheet.setTranslationY(0); } } }); }