这个很简单,看代码就能懂了,就不做描述了:
code
public class BottomSheetFragment extends BottomSheetDialogFragment {
private BottomSheetBehavior<View> mBottomSheetBehavior;
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback
= new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
};
@SuppressLint("RestrictedApi")
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.bottom_sheet_dialog_content_view, null);
dialog.setContentView(contentView);
mBottomSheetBehavior = BottomSheetBehavior.from(((View) contentView.getParent()));
if (mBottomSheetBehavior != null) {
mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
Display display = getActivity().getWindowManager().getDefaultDisplay();
mBottomSheetBehavior.setPeekHeight(display.getHeight() / 2);
contentView.requestLayout();
}
}
}
效果