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

一、首先看一下实现效果

商品规格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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tag="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:id="@+id/pop_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_marginTop="11dp"
            android:id="@+id/goodsRule_msgLinear"
            android:background="@color/backgroundWhite"
            android:paddingBottom="@dimen/toolbar_padLeft"
            android:paddingLeft="@dimen/toolbar_padLeft"
            android:paddingRight="@dimen/toolbar_padLeft"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">
            <ImageView
                android:visibility="invisible"
                android:layout_width="100dp"
                android:layout_height="100dp"/>

            <LinearLayout
                android:paddingLeft="@dimen/toolbar_padLeft"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="¥32"
                    android:textColor="@color/mainColor"
                    android:textSize="14sp"
                    android:textStyle="bold"/>
                <TextView
                    android:layout_marginTop="@dimen/toolbar_padLeft"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="库存 34"
                    android:textColor="@color/textColor06"
                    android:textSize="14sp"/>
                <TextView
                    android:layout_marginTop="@dimen/toolbar_padLeft"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="请选择规格"
                    android:textColor="@color/textColorBlack"
                    android:textSize="14sp"/>
            </LinearLayout>
        </LinearLayout>
        <RelativeLayout
            android:id="@+id/goodsRule_imgId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_marginLeft="@dimen/toolbar_padLeft"
                android:layout_width="100dp"
                android:layout_height="110dp"
                android:src="@mipmap/find_tuijian_ad"
                android:scaleType="centerCrop"/>
        </RelativeLayout>
    </RelativeLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/divide"/>
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:background="@color/backgroundWhite"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:padding="@dimen/toolbar_padLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="颜色"
                android:textSize="14.5sp"
                android:textColor="@color/textColor09"/>
            <com.donkingliang.labels.LabelsView
                android:id="@+id/labels"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/toolbar_padLeft"
                android:paddingRight="@dimen/toolbar_padLeft"
                android:paddingBottom="@dimen/toolbar_padLeft"
                tag:labelBackground="@drawable/label_bg"
                tag:labelTextColor="@drawable/label_text_color"
                tag:labelTextSize="11.5sp"
                tag:labelTextPaddingBottom="4.5dp"
                tag:labelTextPaddingLeft="10dp"
                tag:labelTextPaddingRight="10dp"
                tag:labelTextPaddingTop="4.5dp"
                tag:lineMargin="10dp"
                tag:wordMargin="10dp"
                tag:selectType="SINGLE"
                tag:maxSelect="30" />
            <TextView
                android:padding="@dimen/toolbar_padLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="尺寸"
                android:textSize="14.5sp"
                android:textColor="@color/textColor09"/>
            <com.donkingliang.labels.LabelsView
                android:id="@+id/labels2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/toolbar_padLeft"
                android:paddingRight="@dimen/toolbar_padLeft"
                android:paddingBottom="@dimen/toolbar_padLeft"
                tag:labelBackground="@drawable/label_bg"
                tag:labelTextColor="@drawable/label_text_color"
                tag:labelTextSize="11.5sp"
                tag:labelTextPaddingBottom="4.5dp"
                tag:labelTextPaddingLeft="10dp"
                tag:labelTextPaddingRight="10dp"
                tag:labelTextPaddingTop="4.5dp"
                tag:lineMargin="10dp"
                tag:wordMargin="10dp"
                tag:selectType="SINGLE"
                tag:maxSelect="30" />
            <TextView
                android:padding="@dimen/toolbar_padLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="购买数量"
                android:textSize="14.5sp"
                android:textColor="@color/textColor09"/>
            <LinearLayout
                android:layout_marginLeft="@dimen/toolbar_padLeft"
                android:layout_marginBottom="@dimen/toolbar_padLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:background="@drawable/good_size_buy_num">
                <RelativeLayout
                    android:id="@+id/goodsRule_minusRelative"
                    android:paddingRight="14px"
                    android:paddingLeft="15px"
                    android:paddingBottom="16px"
                    android:paddingTop="16px"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <ImageView
                        android:layout_width="12dp"
                        android:layout_height="12dp"
                        android:src="@mipmap/shopping_cart_minus"/>
                </RelativeLayout>

                <TextView
                    android:id="@+id/goodsRule_numTv"
                    android:text="1"
                    android:textSize="14sp"
                    android:paddingLeft="@dimen/toolbar_padLeft"
                    android:paddingRight="@dimen/toolbar_padLeft"
                    android:textColor="@color/textColor09"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
                <RelativeLayout
                    android:id="@+id/goodsRule_addRelative"
                    android:paddingRight="15px"
                    android:paddingLeft="14px"
                    android:paddingBottom="16px"
                    android:paddingTop="16px"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <ImageView
                        android:layout_width="12dp"
                        android:layout_height="12dp"
                        android:src="@mipmap/shopping_cart_add"/>
                </RelativeLayout>

            </LinearLayout>

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/divide"/>
    <LinearLayout
        android:background="@color/backgroundWhite"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/padding_top"
            android:paddingBottom="@dimen/padding_top"
            android:text="加入购物车"
            android:textSize="32px"
            android:textColor="@color/textColorBlack"/>
        <TextView
            android:gravity="center"
            android:background="@color/mainColor"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/padding_top"
            android:paddingBottom="@dimen/padding_top"
            android:text="立即购买"
            android:textSize="32px"
            android:textColor="@color/textColorWhite"/>
    </LinearLayout>

</LinearLayout>

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<String> 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<String> 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;
            }
        }
    };
百度云下载源码
2561587206401_.pic.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容