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); }
public void bottomOne(View v) { if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) { mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); } else { mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); } }
@Override public void onClick(View v) { behavior.setState( state == BottomSheetBehavior.STATE_COLLAPSED ? BottomSheetBehavior.STATE_EXPANDED : BottomSheetBehavior.STATE_COLLAPSED); }
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); } } }); }