SoFunction
Updated on 2025-04-09

Example of bottom pop-up window packaging for Android DialogFragment

introduction

The above article encapsulates an ordinary dialogfragmentdialog fragmentIt is encapsulated based on Android X DialogFragment.

Based on BottomSheetDialogFragment encapsulation

Compared with ordinary DialogFragment for packaging, the difference is that BottomSheetDialogFragment is specially used for packaging at the bottom pop-up window.

Features

(1) Have animations for appearance (official animation attributes)

(2) Extended BottomViewDialog, and BottomSheetBehavior implement drag-and-drop-related operations, and pull-down to close pop-up windows!

Things to note

(1) Adapting the soft keyboard pops up, you need to set the keyboard parameters through the dialog window. The specific code is as follows:

getDialog().getWindow().setSoftInputMode(.SOFT_INPUT_ADJUST_NOTHING);

Note that if you want to set the outer window keyboard parameter mode through getActivity(), the code is the same as above, and the method of using keyboard monitoring will fail! The specific reason is that you can't listen to the keyboard pop-up event!

(2) When the internal layout is recyclerview, there is a sliding conflict. The following code needs to be manually set to adapt to the Huan conflict event:

().setHasFixedSize(true);
().setNestedScrollingEnabled(false);

The principle is consistent with the scrollview when adapting to the scrollview.

Finally, all the codes are released:

package .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .;
import ;
import .;
import .;
/**
  * FileName: BaseDialogFragment
  * Author: lzt
  * Date: 2022/8/30 17:33
  * change by lzt 20221020 Extract the default width and height initialization method
  */
public abstract class BaseBottomDialogFragment extends BottomSheetDialogFragment {
    private int DEFAULT_WIDTH = .MATCH_PARENT;//Width    private int DEFAULT_HEIGHT = .MATCH_PARENT;//high    private int DEFAULT_GRAVITY = ;//Location    private boolean mCancelable = true;//Cancelable by default    private boolean mCanceledOnTouchOutside = true;//Click external to cancel by default    private BottomSheetBehavior<FrameLayout> bottomSheetBehavior;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mView = (getLayoutId(), container, false);
        initViews(mView);
//        getActivity().getWindow().setSoftInputMode(.SOFT_INPUT_ADJUST_NOTHING);
        if (getDialog() != null && getDialog().getWindow() != null) {
            getDialog().getWindow().setSoftInputMode(.SOFT_INPUT_ADJUST_NOTHING);
        }
        return mView;
    }
    @Override
    public void onDestroyView() {
        clearObserver();
        ();
    }
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        BottomSheetDialog mDialog = (BottomSheetDialog) (savedInstanceState);
        if (null != mDialog) {//initialization            (Window.FEATURE_NO_TITLE);
            (mCanceledOnTouchOutside);
            (mCancelable);
            Window window = ();
            if (null != window) {
                ().setPadding(0, 0, 0, 0);
                (new ColorDrawable());
                 lp = ();
                 = defWidth();
                 = defHeight();
                 = DEFAULT_GRAVITY;
                 = .Animation_InputMethod;
                (lp);
                (dimAmount());
            }
            (new () {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    return !mCancelable;
                }
            });
        }
        return mDialog;
    }
    @Override
    public void onStart() {
        ();
        initSlide();
    }
    /**
      * Initialize sliding interaction
      */
    private void initSlide() {
        if (!canSlide()) {
            return;
        }
        try {
            if (getDialog() != null && getDialog() instanceof BottomSheetDialog) {
                int peekHeight = getPeekHeight() -
                        (getContext());
                BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
                //Remove the default background color of Windows, otherwise the rounded corners will not be displayed.                ().findViewById(.design_bottom_sheet).setBackground(new ColorDrawable());
                //Get the root of the dialog                FrameLayout bottomSheet = ().findViewById(.design_bottom_sheet);
                //Get the LayoutParams object of the root site                 layoutParams = () ();
                 = peekHeight;
                //Modify the maximum height of the pop-up window and do not allow sliding up (slide up by default)                (layoutParams);
                bottomSheetBehavior = (bottomSheet);
                //peekHeight is the maximum height of the pop-up window                (peekHeight);
                //Initially expanded                (BottomSheetBehavior.STATE_EXPANDED);
//                (false);
            }
        } catch (Exception e) {
            ();
        }
    }
    /**
      * Pop-up window height
      * The subclass can override this method to return peekHeight
      *
      * @return height
      */
    protected int getPeekHeight() {
        return getResources().getDisplayMetrics().heightPixels;
    }
    protected float dimAmount() {
        return 0.6f;
    }
    protected int defWidth() {
        return DEFAULT_WIDTH;
    }
    protected int defHeight() {
        return DEFAULT_HEIGHT;
    }
    /**
      * Can it slide
      */
    protected boolean canSlide() {
        return true;
    }
    /**
      * Set location
      *
      * @param gravity
      */
    public void setGravity(int gravity) {
        DEFAULT_GRAVITY = gravity;
    }
    /**
      * Set width
      *
      * @param width
      */
    public void setWidth(int width) {
        DEFAULT_WIDTH = width;
    }
    /**
      * Set high
      *
      * @param height
      */
    public void setHeight(int height) {
        DEFAULT_HEIGHT = height;
    }
    /**
      * Set whether clicking the return button can be cancelled
      *
      * @param cancelable
      */
    public void setCancelable(boolean cancelable) {
        mCancelable = cancelable;
    }
    /**
      * Set whether clicking externally can cancel
      *
      * @param cancelledOnTouchOutside
      */
    public void setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
        mCanceledOnTouchOutside = canceledOnTouchOutside;
    }
    /**
      * Setting up the layout
      *
      * @return
      */
    protected abstract int getLayoutId();
    /**
      * Initialize Views
      *
      * @param v
      */
    protected abstract void initViews(View v);
    //rxjava release----------------------------
    private CompositeDisposable mDisposables = new CompositeDisposable();
    private void addObserver(DisposableObserver<?> disposableObserver) {
        (disposableObserver);
    }
    private void removeObserver(DisposableObserver<?> disposableObserver) {
        if (mDisposables == null || disposableObserver == null) {
            return;
        }
        (disposableObserver);
    }
    private void clearObserver() {
        if (mDisposables != null) {
            ();
        }
    }
    public abstract class BaseSafeObserver<T> extends DisposableObserver<T> {
        @Override
        protected void onStart() {
            ();
            addObserver(this);
        }
        @Override
        public void onNext(@NonNull T t) {
        }
        @Override
        public void onError(@NonNull Throwable e) {
            removeObserver(this);
        }
        @Override
        public void onComplete() {
            removeObserver(this);
        }
    }
    public void releaseResource() {
        clearObserver();
    }
    //rxjava release----------------------------
    /**
      * Can you slide down and close the pop-up window
      */
    protected void setCanSlide(boolean canSlide) {
//        if (bottomSheetBehavior != null) {
//            if (() == canSlide) {
//                return;
//            }
//            (canSlide);
//        }
    }
}

The above is the detailed content of the bottom pop-up example used by Android DialogFragment. For more information about the bottom pop-up window packaging of Android DialogFragment, please pay attention to my other related articles!