preface

As we all know, there are basically two kinds of popups in Android, one is AlertDialog and the other is PopupWindow. The display position of AlertDialog is fixed, and the display position of PopWindow can be set and adjusted. Therefore, some scenes in the project are as follows: Prompt description of a function, click the button to pop up the menu above or below the button, new function pop-up window guidance, etc. Because these popovers are not fixed, you can do them all with PopupWindow. Recently, the PopupWindow function was also used in the project. In the process of writing, I found that although the API was relatively simple, it was still a little tedious to write a PopupWindow. There were a lot of repetitive codes, so I decided to simply encapsulate it. This article is a note and summary of using PopupWindow in a project.

Results the preview

First sacrifice the effect of a look first

The above example shows sevenPopupWindowUsage scenarios

Normal use

We usually configure a PopupWindow as follows:

 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popUpView = inflater.inflate(R.layout.pop_select_num_spec, null.false);
        selectNumSpecPop = new PopupWindow(popUpView, LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
        selectNumSpecPop.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // Set the PopupWindow animation
        selectNumSpecPop.setAnimationStyle(R.style.anim_popUpWindow_bottom);
Copy the code

Then we need to add animations, listen for the back key, etc., and go to findViewById to get the controls inside the Pop, and then, when we need another one, we have to write it again. It’s really tiring.

So the wrapped Pop looks like this:

 popupWindow = new CommonPopupWindow.Builder(getContext())
                .setView(R.layout.pop_window_device)
                .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setAnimationStyle(R.style.pop_add)
                .setViewOnclickListener(this)
                .setOutsideTouchable(true)
                .setBackGroundLevel(0.5 f)// The value ranges from 0.0f to 1.0f
                .builder();
        popupWindow.showAsDropDown(view);
Copy the code

Through the form of Builder to build Pop, this kind of chain writing method, is very familiar with, very simple. Here, I’ve wrapped the pop properties that need to be set in a custom Builder class so I can define them myself

Encapsulated utility class

/ * * *@authorIwen was surprised@create2021/11/09 0:07 * /
public class CommonPopupWindow extends PopupWindow {
​
    public final PopupController controller;
​
    public CommonPopupWindow(PopupController controller) {
        this.controller = controller;
    }
​
    /** * get width */
    @Override
    public int getWidth(a) {
        return controller.mPopupView.getMeasuredWidth();
    }
​
    /** * get high */
    @Override
    public int getHeight(a) {
        return controller.mPopupView.getMeasuredHeight();
    }
​
    /** * hide */
    @Override
    public void dismiss(a) {
        super.dismiss();
        controller.setBackGroundLevel(1.0 f);
    }
​
    /** * View the interface */
    public interface ViewInterface {
        void getChildView(View view, int layoutResId);
    }
​
    /** * constructor **@paramContext Context */
    private CommonPopupWindow(Context context) {
        controller = new PopupController(context, this);
    }
​
    /** * use builder mode */
    public static class Builder {
​
        private final PopupController.PopupParams params;
​
        private ViewInterface listener;
​
        public Builder(Context context) {
            params = new PopupController.PopupParams(context);
        }
​
        /** * PopupWindow **@paramLayoutResId Layout ID *@return Builder
         */
        public Builder setView(int layoutResId) {
            params.mView = null;
            params.layoutResId = layoutResId;
            return this;
        }
​
        /** * Sets the PopupWindow layout **@param view View
         * @return Builder
         */
        public Builder setView(View view) {
            params.mView = view;
            params.layoutResId = 0;
            return this;
        }
​
        /** * sets the child View **@param listener ViewInterface
         * @return Builder
         */
        public Builder setViewOnclickListener(ViewInterface listener) {
            this.listener = listener;
            return this;
        }
​
        /** * Sets the width and height. If not, the default is wrap_content@paramWide width *@return Builder
         */
        public Builder setWidthAndHeight(int width, int height) {
            params.mWidth = width;
            params.mHeight = height;
            return this;
        }
​
        /** * Sets the background gray level **@paramLevel Value range :0.0f to 1.0f The smaller the darker *@return Builder
         */
        public Builder setBackGroundLevel(float level) {
            params.isShowBg = true;
            params.bg_level = level;
            return this;
        }
​
        /** * Can click Outside to disappear **@paramTouchable whether to click *@return Builder
         */
        public Builder setOutsideTouchable(boolean touchable) {
            params.isTouchable = touchable;
            return this;
        }
​
        /** * Set animation **@return Builder
         */
        public Builder setAnimationStyle(int animationStyle) {
            params.isShowAnim = true;
            params.animationStyle = animationStyle;
            return this;
        }
​
        public CommonPopupWindow builder(a){
            final CommonPopupWindow popupWindow = new CommonPopupWindow(params.mContext);
            params.apply(popupWindow.controller);
            if(listener ! =null&& params.layoutResId ! =0){
                listener.getChildView(popupWindow.controller.mPopupView,params.layoutResId);
            }
            UIUtils.measureWidthAndHeight(popupWindow.controller.mPopupView);
            returnpopupWindow; }}}Copy the code

Github

Github.com/ljr7822/And… Welcome to star