Android仿淘宝添加商品时属性规格popupwindow

一、首先看一下实现效果

Android仿淘宝添加商品时属性规格popupwindow_第1张图片
商品规格popupwindow

二、实现步骤

1、自定义标签。(详情请参照android自定义标签)

添加依赖:

在project的build.gradle添加以下代码:

 repositories {

               ...

               maven { url 'https://jitpack.io' }

               }

在module的build.gradle添加以下代码:

dependencies {

              compile 'com.github.donkingliang:LabelsView:1.2.0'

            }

2、Popupwindow的布局文件:popupwindow_goods_rule.xml




    
        
            

            
                
                
                
            
        
        
            
        
    
    
    
        
            
            
            
            
            
            
                
                    
                

                
                
                    
                

            

        
    
    
    
        
        
    


3、自定义GoodSizePopupwindow继承至PopupWindow

public class GoodSizePopupwindow extends PopupWindow{

    private final LabelsView labelsView;
    private final LabelsView labelsView2;
    private final View minusView;
    private final View addView;
    private Context mContext;
    private View view;

    public GoodSizePopupwindow(Context mContext, View.OnClickListener itemsOnClick) {
        this.view = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_goods_rule, null);
        minusView = view.findViewById(R.id.goodsRule_minusRelative);
        addView = view.findViewById(R.id.goodsRule_addRelative);
        labelsView = (LabelsView) view.findViewById(R.id.labels);

//        // 设置按钮监听
        minusView.setOnClickListener(itemsOnClick);
        addView.setOnClickListener(itemsOnClick);

        ArrayList label = new ArrayList<>();
        label.add("白色");
        label.add("黑色");
        label.add("图片色");
        label.add("藕粉色");
        label.add("卡其色");
        label.add("浅蓝色(比图片较深,介意勿拍)");
        label.add("粉色");
        labelsView.setLabels(label); //直接设置一个字符串数组就可以了。
//        ======================================================
        labelsView2 = (LabelsView) view.findViewById(R.id.labels2);
        ArrayList label2 = new ArrayList<>();
        label2.add("34");
        label2.add("35");
        label2.add("36(尺码偏小,请拍小一号)");
        label2.add("37");
        label2.add("38");
        label2.add("39");
        label2.add("40");
        label2.add("41");
        label2.add("42");
        labelsView2.setLabels(label2); //直接设置一个字符串数组就可以了。

        // 设置外部可点击
        this.setOutsideTouchable(true);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        this.view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = view.findViewById(R.id.pop_layout).getTop();

                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });


    /* 设置弹出窗口特征 */
        // 设置视图
        this.setContentView(this.view);
        // 设置弹出窗体的宽和高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);

        // 设置弹出窗体可点击
        this.setFocusable(true);
        this.setBackgroundDrawable(new BitmapDrawable());

        // 设置弹出窗体显示时的动画,从底部向上弹出
        this.setAnimationStyle(R.style.take_photo_anim);

    }

}

4、PopupWindow的使用

private void showPopupwindow() {
        GoodSizePopupwindow sizePopWin = new GoodSizePopupwindow(this, onClickListener);
        View contentView = sizePopWin.getContentView();
        addCartNumTv = ((TextView) contentView.findViewById(R.id.goodsRule_numTv));
        //设置Popupwindow显示位置(从底部弹出)
        sizePopWin.showAtLocation(mainLayout, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
        //当弹出Popupwindow时,背景变半透明
        darkenBackgroud(0.4f);
        //设置Popupwindow关闭监听,当Popupwindow关闭,背景恢复1f
        sizePopWin.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                darkenBackgroud(1f);
            }
        });
    }

5、popupWindow中的点击事件

private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.goodsRule_minusRelative:
                    int count = Integer.valueOf((String)addCartNumTv.getText());
                    if(count==1){
                        Toast.makeText(MainActivity.this,"不能再减了哦",Toast.LENGTH_SHORT).show();
                    }else{
                        count--;
                        addCartNumTv.setText((count)+"");
                    }
                    break;
                case R.id.goodsRule_addRelative:
                    int count2 = Integer.valueOf((String)addCartNumTv.getText());
                    count2++;
                    addCartNumTv.setText(count2+"");
                    break;
            }
        }
    };

你可能感兴趣的:(Android仿淘宝添加商品时属性规格popupwindow)