打造自定义弹出框

自定义弹出框的使用还是比较频繁,本次主要讲解自定义弹出框和使用的封装,通过接口利于维护。
首先给出自定义弹出框的实现:


public class CustomPopupWindow extends PopupWindow implements OnTouchListener{

        protected View mPopupView;  
        protected Context context;
        /**
         * PopupWindow显示在哪个view的下面
         */
        protected View locationView;
        protected LayoutInflater inflater;
        protected int _popupWindow_Layout_ID = -1;

        public View getPopupWindowView(){
            return mPopupView;
        }
        /**
         * 
         * @param _popupWindow_Layout_ID:需要显示的视图的布局id
         * @param viewLayoutLocation_ID:视图显示在哪个布局的下面
         */
        public void toShowPopupWindowLayout(int _popupWindow_Layout_ID, int viewLayoutLocation_ID){
            if (this._popupWindow_Layout_ID  == -1){
                this._popupWindow_Layout_ID = _popupWindow_Layout_ID;
            }
            configPopupWindowView();
            showPopupWindow(viewLayoutLocation_ID);
        }

        public void toShowPopupWindowLayout(int _popupWindow_Layout_ID, int viewLayoutLocation_ID,
                                            PopupWindowLayoutParams params){
            if (this._popupWindow_Layout_ID  == -1){
                this._popupWindow_Layout_ID = _popupWindow_Layout_ID;
            }
            if (inflater == null){
                inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }
            mPopupView = inflater.inflate(_popupWindow_Layout_ID, null);

            this.setContentView(mPopupView);
            if (params == PopupWindowLayoutParams.wrap_parent){
                this.setWidth(LayoutParams.WRAP_CONTENT);
            } else if (params == PopupWindowLayoutParams.match_parent){
                this.setWidth(LayoutParams.MATCH_PARENT);
            }
            this.setHeight(LayoutParams.WRAP_CONTENT);
            mPopupView.setFocusable(true);
            mPopupView.setFocusableInTouchMode(true);
            this.setOutsideTouchable(false);
            this.setFocusable(false);
            this.setTouchable(true);

            this.setAnimationStyle(R.style.anim_menu_bottombar);
            ColorDrawable dw = new ColorDrawable(0xEEEEEE);//实例化一个ColorDrawable颜色为0xb0000000半透明
            this.setBackgroundDrawable(dw); //设置弹出窗体的背景
            mPopupView.setOnKeyListener(new OnKeyListener() {


                @Override
                public boolean onKey(View arg0, int keyCode, KeyEvent arg2) {
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        CustomPopupWindow.this.dismiss();
                        return true;
                    }
                    return false;
                }
            });
            showPopupWindow(viewLayoutLocation_ID);

        }

        public enum PopupWindowLayoutParams{
            wrap_parent, match_parent
        }

        private void configPopupWindowView(){
            if (inflater != null){
                return;
            }
            inflater = (LayoutInflater) context  
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
            mPopupView = inflater.inflate(_popupWindow_Layout_ID, null);  

            this.setContentView(mPopupView);  
            this.setWidth(LayoutParams.WRAP_CONTENT);  
            this.setHeight(LayoutParams.WRAP_CONTENT); 
            mPopupView.setFocusable(true); 
            mPopupView.setFocusableInTouchMode(true);
            this.setOutsideTouchable(false);
            this.setFocusable(false);
            this.setTouchable(true);

            this.setAnimationStyle(R.style.anim_menu_bottombar);
            ColorDrawable dw = new ColorDrawable(0xEEEEEE);//实例化一个ColorDrawable颜色为0xb0000000半透明               
            this.setBackgroundDrawable(dw); //设置弹出窗体的背景 
            mPopupView.setOnKeyListener(new OnKeyListener() {


                @Override
                public boolean onKey(View arg0, int keyCode, KeyEvent arg2) {
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        CustomPopupWindow.this.dismiss();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                        return true;
                    }
                    return false;
                }
            });
        }

        public CustomPopupWindow(Context context) {  
            super(context);  
            this.context = context;
        }

        private void showPopupWindow(int viewLayoutLocation_ID){
            locationView = inflater.inflate(viewLayoutLocation_ID, null);
            this.showAtLocation(locationView,
                     Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); 
        }


        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            if (arg1.getAction() == MotionEvent.ACTION_OUTSIDE){
                boolean isShow = this.isShowing();
                if (isShow){
                    // 不做处理
                }
            }
            return false;
        }

}

首先定义接口分别用于显示和不显示弹出框:

public interface IPopupWindowManager {

    void disposePopupWindowToShow();
    void dismissPopupWindow();
}

实现接口并注入弹出框对象,并且回调点击事件


public class PopupWindowManagerImpl implements IPopupWindowManager{

    private CustomPopupWindow customPopupWindow;
    private Context context;
    private Button btn_popupwindow_cancel_id;

    private OnPopupWindowClickListener listener;

    public PopupWindowManagerImpl(Context context, OnPopupWindowClickListener listener){
        this.context = context;
        this.listener = listener;
    }

    @Override
    public void disposePopupWindowToShow() {
        if (customPopupWindow == null){
            customPopupWindow = new CustomPopupWindow(context);
        }
        customPopupWindow.toShowPopupWindowLayout(R.layout.popupwindow_view, R.layout.download_files_queue_activity,
                CustomPopupWindow.PopupWindowLayoutParams.match_parent);
        btn_popupwindow_cancel_id = (Button) customPopupWindow.getPopupWindowView().findViewById(R.id.popupwindow_cancel_id);

        btn_popupwindow_cancel_id.setText(Application.getAppString(R.string.cancel_down_queue));
        btn_popupwindow_cancel_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onPopupWindowClick(v);
            }
        });

    }

    @Override
    public void dismissPopupWindow() {
        if (customPopupWindow != null && customPopupWindow.isShowing()){
            customPopupWindow.dismiss();
        }
    }

    public interface OnPopupWindowClickListener{
        void onPopupWindowClick(View view);
    }


}

剩下的就是使用接口调用方法来完成逻辑业务了。你可能会有不同的界面需求,那么你只需要仿照上面的接口实现类来做就行,当然这需要花上几分钟阅读一下代码。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值