Dim Screen and Block Interaction with BottomSheets

北城余情 提交于 2019-12-05 05:43:14
azizbekian
public class BottomSheetDimmedFragment extends BottomSheetDialogFragment {
    public static final String TAG = BottomSheetDimmedFragment.class.getSimpleName();

    @NonNull
    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        final BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
        final View view = View.inflate(getContext(), R.layout.test, null);
        dialog.setContentView(view);
        return dialog;
    }

    public void show(final FragmentActivity fragmentActivity) {
        show(fragmentActivity.getSupportFragmentManager(), TAG);
    }
}

In your activity:

BottomSheetDimmedFragment sheet = new BottomSheetDimmedFragment();
sheet.show(this);

Now, you will have a dim and also when clicked on a dim the dialog will close.

Implementation taken from here.

Use the bottom sheet with a fragment instead of a view :)

Note that there are two implementations:

BottomSheetBehavior and BottomSheetDialogFragment.

Use BottomSheetDialogFragment to get the functionality you need.

Also when using BottomSheetBehavior set the layout's android:clickable="true". That way clicks don't go through when you click on empty space. (For clarity: clickable is set on the layout containing the tag app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior")

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!