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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值