欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-31 10:56:08
...

效果图如下:
PopupWindow里面动态添加内容的应用

核心代码:
 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的弹出动画:
 <style name="popup_anim_style">
        <item name="android:windowEnterAnimation">@anim/pop_enter_anim</item>
        <item name="android:windowExitAnimation">@anim/pop_exit_anim</item>
        <item name="android:background">#00ffffff</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

pop_enter_anim的内容:
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="200"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set> 

控制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

相关标签: PopupWindow