Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

android - BottomDialogFragment shows a white screen when opened

I made a base class which extends from BottomDialogFragment, it inflates a layout which has a placeholder FrameLayout on which you can put which ever fragment you want.

That way I have a consistent layout for bottom dialog fragments and you don't need to adapt every fragment to be a bottom dialog fragment.

The problem is whenever I call .show() on the fragment, my whole screen turns white and you can't see anything or do anything except going back which closes the fragment. (The whole screen not only the bottom fragment space)

The BaseBottomDialogFragment code:

public abstract class BaseBottomSheetDialogFragment extends BottomSheetDialogFragment implements MvpView {

    private static final int PERMISSION_RC = 10;

    private boolean mIsShown = false;

    protected String[] mRequiredPermissions;

    protected BaseActivity mActivity;

    protected FragmentBottomDialogBinding binding;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof BaseActivity) {
            this.mActivity = (BaseActivity) context;
            mActivity.onAttachFragment(this);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentBottomDialogBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequiredPermissions = onGetPermissions();
        onSetup();
    }

    @Override
    public void show(@NonNull FragmentManager manager, @Nullable String tag) {
        if (!mIsShown) {
            super.show(manager, tag);
            this.mIsShown = true;
        }
    }

    @Override
    public void dismiss() {
        if (mIsShown) {
            super.dismiss();
            this.mIsShown = false;
        }
    }

    @Override
    public String[] onGetPermissions() {
        return new String[]{};
    }

    @Override
    public boolean hasPermission(String permission) {
        return false;
    }

    @Override
    public boolean hasPermissions() {
        for (String permission : mRequiredPermissions) {
            if (!mActivity.hasPermission(permission)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void askForPermissions() {
        requestPermissions(mRequiredPermissions, PERMISSION_RC);
    }

    @Override
    public void showMessage(String message) {
        if (mActivity != null) {
            mActivity.showMessage(message);
        }
    }

    @Override
    public void showMessage(@StringRes int stringResId) {
        if (mActivity != null) {
            mActivity.showMessage(stringResId);
        }
    }

    @Override
    public void showError(String message) {
        if (mActivity != null) {
            mActivity.showError(message);
        }
    }

    @Override
    public void showError(@StringRes int stringResId) {
        if (mActivity != null) {
            mActivity.showError(getString(stringResId));
        }
    }

    protected void putFragment(BaseFragment fragment) {
        mActivity.getSupportFragmentManager().beginTransaction().replace(binding.bottomDialogFragmentPlaceholder.getId(), fragment);
    }

    protected Bundle getArgsSafely() {
        Bundle args = super.getArguments();
        if (args == null) {
            throw new IllegalArgumentException("You must create this fragment with newInstance()");
        }
        return args;
    }

    public boolean ismIsShown() {
        return mIsShown;
    }
}

The layout code for the BaseBottomDialogFragment:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_dialog_fragment_layout"
    style="@style/Appbar.Layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <View
        android:id="@+id/bottom_dialog_fragment_swipe_indicator"
        android:layout_width="@dimen/swipe_indicator_width"
        android:layout_height="@dimen/swipe_indicator_height"
        android:layout_margin="@dimen/swipe_indicator_margin"
        android:background="@drawable/swipe_indicator_background"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/bottom_dialog_fragment_placeholder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bottom_dialog_fragment_swipe_indicator" />
</androidx.constraintlayout.widget.ConstraintLayout>

Example for a class using BaseBottomDialogFragment:

public class ContactBottomDialogFragment extends BaseBottomSheetDialogFragment {

    public static final String TAG = "contact_bottom_dialog_fragment";
    public static final String ARG_CONTACT = "contact";

    private ContactFragment mContactFragment;

    public static ContactBottomDialogFragment newInstance(Contact contact) {
        Bundle args = new Bundle();
        args.putSerializable(ARG_CONTACT, contact);
        ContactBottomDialogFragment fragment = new ContactBottomDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onSetup() {
        mContactFragment = ContactFragment.newInstance((Contact) getArgsSafely().getSerializable(ARG_CONTACT));
        putFragment(mContactFragment);
    }
}

And I would call show() for the class above for example.

Thank you :).

question from:https://stackoverflow.com/questions/65647221/bottomdialogfragment-shows-a-white-screen-when-opened

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...