PopupWindow里面动态添加内容的应用

效果图如下:
PopupWindow里面动态添加内容的应用_第1张图片

核心代码:
 private void showDialog() {
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        View bordView = layoutInflater.inflate(R.layout.goods_select_pop_up, null);

        goodsSelectLayout = (LinearLayout) bordView.findViewById(R.id.goods_container);
        goodsSelectTitleTv = (TextView) bordView.findViewById(R.id.goods_select_title);
        int width = getResources().getDisplayMetrics().widthPixels;
        goodSelectBoard = new PopupWindow(bordView, width, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        goodSelectBoard.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        goodSelectBoard.setAnimationStyle(R.style.popup_anim_style);
        goodSelectBoard.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setMaskDisplay(false);
            }
        });

        goodsSelectListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goodSelectBoard.dismiss();
            }
        };
        // 2. 更新內容
        goodsSelectLayout.removeAllViews();
        int num = dataList.size();
        for (int i = 0; i < num; i++) {
            String barcode = dataList.get(i);
            View itemView = layoutInflater.inflate(R.layout.goods_select_item, null);
            itemView.setOnClickListener(goodsSelectListener);
            TextView nameTv = (TextView) itemView.findViewById(R.id.goods_item_name);
            nameTv.setText(barcode);
            goodsSelectLayout.addView(itemView);
        }

        // 3. 显示popupWindow
        goodSelectBoard.showAtLocation(rootView, Gravity.CENTER, 0, 0);
        setMaskDisplay(true);
    }

PopupWindow的弹出动画:
 

pop_enter_anim的内容:

    
 

控制PopupWindow的遮罩的出现和消失:
   private void setMaskDisplay(boolean display) {
        if (null != maskAnimator1 && maskAnimator1.isRunning()) {
            maskAnimator1.cancel();
        }
        float alphaTo = display ? 1.0f : 0.0f;
        float alphaFrom = 1.0f - alphaTo;
        maskAnimator1 = ObjectAnimator.ofFloat(maskView, "alpha", alphaFrom, alphaTo);
        maskAnimator1.setDuration(200);
        maskAnimator1.start();

    }
代码下载地址:https://download.csdn.net/download/jingerlovexiaojie/10375298

你可能感兴趣的:(Android)