Android 购物选择颜色、尺码实现

在做一个项目的时候需要选择商品的规格,在参考了一些相似功能的实现之后,自己结合项目需求自定义了一个弹窗。下面是实现该功能的截图 及 相关代码。


1、实现这个功能主要用了流式布局FlowTagLayout 来展示规格 

参照我的一篇文章 点击打开链接

2、弹窗布局 pop_categary_chose.xml

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

    <RelativeLayout
        android:id="@+id/rl_relative"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent">
        <LinearLayout
            android:id="@+id/ll_product"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:orientation="vertical"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="40dp">

                <TextView
                    android:id="@+id/tv_buy_price"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="120dp"
                    android:text="¥0"
                    android:layout_gravity="center_vertical"
                    android:gravity="center_vertical"
                    android:textColor="@color/gridview_text_red"
                    android:textSize="@dimen/gridview_text_size" />

                <ImageView
                    android:id="@+id/iv_close"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@mipmap/ic_clear"
                    android:layout_margin="10dp"/>

            </LinearLayout>
            <TextView
                android:id="@+id/tv_stock"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="bottom"
                android:layout_marginLeft="120dp"
                android:text="库存0件"
                android:textColor="@color/c_666666"
                android:textSize="@dimen/gridview_text_size"
                android:layout_weight="1"/>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="120dp"
                android:orientation="horizontal">
            <TextView
                android:id="@+id/tv_desc_tip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:textColor="@color/c_666666"
                android:text="请选择规格"
                android:textSize="14dp"/>
                <TextView
                    android:id="@+id/tv_chosed_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:textColor="@color/c_666666"
                    android:text=""
                    android:textSize="14dp"/>
            </LinearLayout>

            <View
                android:id="@+id/line1"
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="20dp"
                android:background="@color/line_bg"
                android:visibility="gone" />

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="250dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:orientation="vertical">
            <LinearLayout
                android:id="@+id/ll_size1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="vertical"
                android:paddingLeft="15dp"
                android:paddingRight="10dp"
                android:layout_marginTop="8dp">
                <!--动态加入备选项-->
                <TextView
                    android:id="@+id/tv_size1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:text="颜色"
                    android:layout_marginTop="20dp"
                    android:textSize="@dimen/gridview_text_size"
                    android:textColor="@color/c_666666"/>
                <com.xj.hpqq.huopinquanqiu.widget.flowtaglayout.FlowTagLayout
                    android:id="@+id/fl_desc1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <View
                android:id="@+id/line2"
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="20dp"
                android:background="@color/line_bg"
                android:visibility="gone" />

            <LinearLayout
                android:id="@+id/ll_size2"
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:paddingLeft="15dp"
                android:paddingRight="10dp"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:layout_marginTop="8dp">
                <TextView
                    android:id="@+id/tv_size2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:text="尺寸"
                    android:layout_marginTop="20dp"
                    android:textSize="@dimen/gridview_text_size"
                    android:textColor="@color/c_666666"/>

                <!--动态加入备选项-->
                <com.xj.hpqq.huopinquanqiu.widget.flowtaglayout.FlowTagLayout
                    android:id="@+id/fl_desc2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <View
                android:id="@+id/line3"
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="20dp"
                android:background="@color/line_bg"
                android:visibility="visible" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:paddingTop="10dp"
                android:paddingBottom="10dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:text="购买数量"
                    android:visibility="invisible"
                    android:textSize="16dp"
                    android:textColor="@color/c_4E4E4E"
                    android:layout_weight="1"/>

                <TextView
                    android:id="@+id/tv_reduce"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="—"
                    android:background="@drawable/button_shape_left_ring_gray"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:textSize="20dp" />

                <TextView
                    android:id="@+id/tv_number"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1"
                    android:textSize="20dp"
                    android:textColor="@color/c_666666"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="15dp"/>

                <TextView
                    android:id="@+id/tv_add"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="+"
                    android:background="@drawable/button_shape_right_ring_gray"
                    android:paddingLeft="12dp"
                    android:paddingRight="12dp"
                    android:textSize="20dp"
                    android:layout_marginRight="15dp"/>

            </LinearLayout>
        </LinearLayout>
            </ScrollView>
            <TextView
                android:id="@+id/tv_ok"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@color/gridview_text_red"
                android:text="确定"
                android:gravity="center"
                android:textSize="@dimen/gridview_text_size"
                android:textColor="@color/white"/>

        </LinearLayout>

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/iv_product_icon"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_alignTop="@+id/ll_product"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="-20dp"
            android:visibility="visible"
            fresco:roundBottomLeft="true"
            fresco:roundBottomRight="true"
            fresco:roundTopLeft="true"
            fresco:roundTopRight="true"
            fresco:roundedCornerRadius="5dip"
            fresco:roundingBorderColor="@color/line_bg"
            fresco:roundingBorderWidth="1dip"
            fresco:actualImageScaleType="centerCrop"
            fresco:failureImage="@mipmap/ic_default"
            fresco:failureImageScaleType="centerCrop"/>

    </RelativeLayout>
</LinearLayout>

3、新建dialog弹窗 设置布局 初始化各个控件

int goodsCount = 1;
int specId = 0;
private String desc1 = "", desc2 = "", defaultDesc = "";
private int realStock = 0;
private TagAdapter<TagBean> mSizeTagAdapter1, mSizeTagAdapter2;
private ArrayList<ArrayList<TagBean>> spceList = new ArrayList<>();//所有的二级规格列表的集合
private ArrayList<TagBean> listDesc1 = new ArrayList<>();//一级规格列表
private ArrayList<TagBean> listDesc2 = new ArrayList<>();//选中的一级规格对应的二级规格列表
private AlertDialog dialog; 
List<String> data1 = new ArrayList<>();//第一规格名称

private void showCatePopupWindow(View view) {
        dialog = new AlertDialog.Builder(this).create();//生成一个AlertDialog对象
        dialog.show();//调用AlertDialog的show()方法显示
        dialog.setCanceledOnTouchOutside(true);
        View contentView = null;
        // 一个自定义的布局,作为显示的内容
        contentView = LayoutInflater.from(this).inflate(
                R.layout.pop_categary_chose, null);
        final TextView tvDescTip = (TextView) contentView.findViewById(R.id.tv_desc_tip);
        final TextView tvPrice = (TextView) contentView.findViewById(R.id.tv_buy_price);
        ImageView ivClose = (ImageView) contentView.findViewById(R.id.iv_close);
        final TextView tvStock = (TextView) contentView.findViewById(R.id.tv_stock);
        final TextView tvChosedDesc = (TextView) contentView.findViewById(R.id.tv_chosed_desc);
        TextView tvSize1 = (TextView) contentView.findViewById(R.id.tv_size1);
        TextView tvSize2 = (TextView) contentView.findViewById(R.id.tv_size2);
        SimpleDraweeView ivProductImage = (SimpleDraweeView) contentView.findViewById(R.id.iv_product_icon);
        FlowTagLayout flowLayout1 = (FlowTagLayout) contentView.findViewById(R.id.fl_desc1);
        final FlowTagLayout flowLayout2 = (FlowTagLayout) contentView.findViewById(R.id.fl_desc2);
        TextView tvReduce = (TextView) contentView.findViewById(R.id.tv_reduce);
        final TextView tvNumber = (TextView) contentView.findViewById(R.id.tv_number);
        TextView tvAdd = (TextView) contentView.findViewById(R.id.tv_add);
        TextView tvOk = (TextView) contentView.findViewById(R.id.tv_ok);
        llSize1 = (LinearLayout) contentView.findViewById(R.id.ll_size1);
        llSize2 = (LinearLayout) contentView.findViewById(R.id.ll_size2);
        Window window = dialog.getWindow();//获取当前的Window对象,然后下面进行窗口属性的设置
        window.setContentView(contentView);//加载布局,view是填充自定义菜单布局xml 得到的
        window.setBackgroundDrawableResource(android.R.color.transparent);//这个很重要,将背景设为透明
        window.setGravity(Gravity.BOTTOM);//这个也很重要,将弹出菜单的位置设置为底部
        window.setWindowAnimations(R.style.dialog_style);//菜单进入和退出屏幕的动画,实现了上下滑动的动画效果
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);//设置菜单的尺寸
        data1.clear();
        listDesc1.clear();//一级规格列表
        listDesc2.clear();//选中的一级规格对应的二级规格列表
        desc1 = "";//选中的规格一
        desc2 = "";//选中的规格二
        spceList.clear();//所有的二级规格列表的集合
        if (shopGoodsInfoDetailBean.getProduct().getSpecTitle() != null) {
            llSize1.setVisibility(View.VISIBLE);
            tvSize1.setText(shopGoodsInfoDetailBean.getProduct().getSpecTitle());
            for (int i = 0; i < shopGoodsInfoDetailBean.getProduct().getSpecs().size(); i++) {
                if (!data1.contains(shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getSpecValue())) {//根据自己的数据进行整理将所有的一级规格筛选出来放到列表中
                    TagBean tagBean = new TagBean();
                    tagBean.setDesc(shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getSpecValue());
                    if (shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getRealStock() == 0) {
                        tagBean.setTag(1);  //bean类中有个标记  来判断当前规格的库存是否为0 如果为0 tag设为1  则不可点击无法选中 否则设为0 
                    } else {
                        tagBean.setTag(0);
                    }
                    listDesc1.add(tagBean);
                    data1.add(shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getSpecValue());
                }
            }
        } else {
            llSize1.setVisibility(View.GONE);
        }


        if (!TextUtils.isEmpty(shopGoodsInfoDetailBean.getProduct().getSpecTitle2()) && !shopGoodsInfoDetailBean.getProduct().getSpecTitle2().equals("")) {//判断第二规格是否有 然后才处理第二规格的数据 
            llSize2.setVisibility(View.VISIBLE);
            tvSize2.setText(shopGoodsInfoDetailBean.getProduct().getSpecTitle2());
            for (int j = 0; j < data1.size(); j++) {
                ArrayList<TagBean> list = new ArrayList<>();
                for (int k = 0; k < shopGoodsInfoDetailBean.getProduct().getSpecs().size(); k++) {
                    if (data1.get(j).equals(shopGoodsInfoDetailBean.getProduct().getSpecs().get(k).getSpecValue())) {//根据一级规格分别筛选对应的二级规格
                        TagBean tagBean = new TagBean();
                        tagBean.setDesc(shopGoodsInfoDetailBean.getProduct().getSpecs().get(k).getSpecValue2());
                        if (shopGoodsInfoDetailBean.getProduct().getSpecs().get(k).getRealStock() == 0) {
                            tagBean.setTag(1);
                            tagBean.setCheckTag(0);
                        } else {
                            tagBean.setTag(0);
                            tagBean.setCheckTag(0);
                        }
                        list.add(tagBean);
                    }
                }
                spceList.add(list);
            }
        } else {
            llSize2.setVisibility(View.GONE);
        }

        mSizeTagAdapter1 = new TagAdapter<TagBean>(OtherFragmentGoodsInfoAcitivty.this);//初始adapter
        flowLayout1
                .setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        flowLayout1.setAdapter(mSizeTagAdapter1);
        mSizeTagAdapter1.onlyAddAll(listDesc1);
        if (llSize2.getVisibility() == View.VISIBLE) {
            listDesc2.addAll(spceList.get(0));
            mSizeTagAdapter2 = new TagAdapter<TagBean>(OtherFragmentGoodsInfoAcitivty.this);
            flowLayout2
                    .setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
            flowLayout2.setAdapter(mSizeTagAdapter2);
            mSizeTagAdapter2.onlyAddAll(listDesc2);//这一句式添加数据 会自动set数据并刷新布局
        }


        //规格1
        flowLayout1.setOnTagSelectListener(new OnTagSelectListener() {//每种规格点击事件监听
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                //ArrayList<TagBean> newList = spceList.get(selectedList.get(0));
                if (listDesc1.get(selectedList.get(0)).getTag() == 0) {
                    for (int i = 0; i < listDesc1.size(); i++) {
                        if (i == selectedList.get(0)) {
                            listDesc1.get(i).setCheckTag(1);
                        } else {
                            listDesc1.get(i).setCheckTag(0);
                        }
                    }
                    defaultDesc = desc2;
                    if (tvDescTip.getText().toString().equals("请选择规格")) {
                        tvDescTip.setText("已选规格:");
                    }
                    desc1 = listDesc1.get(selectedList.get(0)).getDesc();
                    mSizeTagAdapter1.notifyDataSetChanged();
                    if (llSize2.getVisibility() == View.VISIBLE) {
                        listDesc2.clear();
                        listDesc2.addAll(spceList.get(selectedList.get(0)));
                        for (int i = 0; i < listDesc2.size(); i++) {
                            if (listDesc2.get(i).getTag() != 1) {
                                if (listDesc2.get(i).getDesc().equals(defaultDesc)) {
                                    listDesc2.get(i).setCheckTag(1);
                                } else {
                                    listDesc2.get(i).setCheckTag(0);
                                }
                            }
                        }
                        mSizeTagAdapter2.onlyAddAll(listDesc2);
                        if (!TextUtils.isEmpty(desc2)) {
                            for (int i = 0; i < shopGoodsInfoDetailBean.getProduct().getSpecs().size(); i++) {
                                if (shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getSpecValue().equals(desc1) && shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getSpecValue2().equals(desc2)) {
                                    tvStock.setText("库存" + shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getRealStock() + "件");
                                    stokes = shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getRealStock();
                                    if (isG) {
                                        tvPrice.setText("¥" + shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getGroupPrice());
                                    } else {
                                        tvPrice.setText("¥" + shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getPrice());
                                    }
                                    break;
                                }
                            }
                        }
                    } else {
                        for (int i = 0; i < shopGoodsInfoDetailBean.getProduct().getSpecs().size(); i++) {
                            if (desc1.equals(shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getSpecValue())) {
                                tvStock.setText("库存" + shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getRealStock() + "件");
                                stokes = shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getRealStock();
                                if (isG) {
                                    tvPrice.setText("¥" + shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getGroupPrice());
                                } else {
                                    tvPrice.setText("¥" + shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getPrice());
                                }
                            }
                        }
                    }
              

                    if (desc2 == "") {
                        tvChosedDesc.setText(desc1);
                    } else {
                        tvChosedDesc.setText(desc1 + "," + desc2);
                    }
                }
            }
        });

        //规格2
        flowLayout2.setOnTagSelectListener(new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                if (listDesc2.get(selectedList.get(0)).getTag() == 0) {
                    for (int i = 0; i < listDesc2.size(); i++) {
                        if (i == selectedList.get(0)) {
                            listDesc2.get(i).setCheckTag(1);
                        } else {
                            listDesc2.get(i).setCheckTag(0);
                        }
                    }
                    mSizeTagAdapter2.notifyDataSetChanged();
                    if (tvDescTip.getText().toString().equals("请选择规格")) {
                        tvDescTip.setText("已选规格:");
                    }
                    if (selectedList != null && selectedList.size() > 0) {
                        desc2 = listDesc2.get(selectedList.get(0)).getDesc();
                        for (int i = 0; i < shopGoodsInfoDetailBean.getProduct().getSpecs().size(); i++) {
                            if (shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getSpecValue().equals(desc1) && shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getSpecValue2().equals(desc2)) {
                                tvStock.setText("库存" + shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getRealStock() + "件");
                                stokes = shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getRealStock();
                                if (isG) {
                                    tvPrice.setText("¥" + shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getGroupPrice());
                                } else {
                                    tvPrice.setText("¥" + shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getPrice());
                                }

                                break;
                            }
                        }
                    } else {
                        desc2 = "";
                    }
                    tvChosedDesc.setText(desc1 + "," + desc2);
                }
            }
        });

        tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goodsCount++;
                tvNumber.setText(goodsCount + "");
            }
        });

        tvReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (goodsCount > 1) {
                    goodsCount--;
                    tvNumber.setText(goodsCount + "");
                }
            }
        });

        tvNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(desc1)) {
                    ToastUtil.showToast("请选择规格");
                } else {
                    if (spceList.size() != 0) {
                        if (TextUtils.isEmpty(desc2)) {
                            ToastUtil.showToast("请选择规格");
                        } else {
                            for (int i = 0; i < shopGoodsInfoDetailBean.getProduct().getSpecs().size(); i++) {
                                if (!desc2.equals("")) {
                                    if (shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getSpecValue().equals(desc1) && shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getSpecValue2().equals(desc2)) {
                                        specId = shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getId();
                                    }
                                } else {
                                    if (shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getSpecValue().equals(desc1)) {
                                        specId = shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getId();
                                    }
                                }
                            }
                            if (stokes == 0) {
                                ToastUtil.showToast("商品已售罄");
                            } else {
                                doRequestOrderInfo();
                                dialog.dismiss();
                                goodsCount = 1;
                            }
                        }
                    } else {
                        for (int i = 0; i < shopGoodsInfoDetailBean.getProduct().getSpecs().size(); i++) {
                            if (shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getSpecValue().equals(desc1)) {
                                specId = shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getId();
                            }
                        }
                        doRequestOrderInfo();
                        dialog.dismiss();
                        goodsCount = 1;
                    }
                }
            }
        });

        ivProductImage.setImageURI(Uri.parse(AppConstants.BASE_IMAGE_URL + shopGoodsInfoDetailBean.getProduct().getImgUrl()));
        double price = 0;
        int stock = 0;
        if (isG) {
            for (int i = 0; i < listDesc1.size(); i++) {
                if (listDesc1.get(i).getTag() == 0) {
                    price = shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getGroupPrice();
                    stock = shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getRealStock();
                    break;
                }
            }
        } else {
            for (int i = 0; i < listDesc1.size(); i++) {
                if (listDesc1.get(i).getTag() == 0) {
                    price = shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getPrice();
                    stock = shopGoodsInfoDetailBean.getProduct().getSpecs().get(i).getRealStock();
                    break;
                }
            }
        }
        tvPrice.setText("¥" + price);
        tvStock.setText("库存" + stock + "件");
        stokes = stock;
        SimpleDraweeView simpleDraweeView = (SimpleDraweeView) contentView.findViewById(R.id.iv_product_icon);
        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goodsCount = 1;
                dialog.dismiss();
            }
        });
    }

3.1  TagAdapter 


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xj.hpqq.huopinquanqiu.R;
import com.xj.hpqq.huopinquanqiu.bean.TagBean;
import com.xj.hpqq.huopinquanqiu.util.SpUtil;
import com.xj.hpqq.huopinquanqiu.widget.flowtaglayout.OnInitSelectedPosition;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HanHailong on 15/10/19.
 */
public class TagAdapter<T> extends BaseAdapter implements OnInitSelectedPosition {

    private final Context mContext;
    private final List<T> mDataList;

    public TagAdapter(Context context) {
        this.mContext = context;
        mDataList = new ArrayList<T>();
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.tag_item, null);

        TextView textView = (TextView) view.findViewById(R.id.tv_tag);
        T t = mDataList.get(position);

        if (t instanceof TagBean) {
            textView.setText(((TagBean) t).getDesc());
            if(((TagBean) t).getTag()==1){
                //textView.setClickable(false);
                textView.setBackground(mContext.getResources().getDrawable(R.drawable.tag_nochecked_bg));
                textView.setTextColor(mContext.getResources().getColor(R.color.tag_textcolor1));
            }else {
                //textView.setClickable(true);
                if (((TagBean) t).getCheckTag() == 1) {
                    //Log.w("=======",((TagBean) t).getDesc()+"");
                    textView.setBackground(mContext.getResources().getDrawable(R.drawable.tag_checked_bg));
                    textView.setTextColor(mContext.getResources().getColor(R.color.white));
                } else {
                    //textView.setClickable(false);
                    textView.setBackground(mContext.getResources().getDrawable(R.drawable.tag_unchecked_bg));
                    textView.setTextColor(mContext.getResources().getColor(R.color.c_666666));
                }
            }
        }

//        if(t instanceof String){
//            textView.setText((String)t);
//        }

        return view;
    }

    public void onlyAddAll(List<T> datas) {
        mDataList.clear();
        mDataList.addAll(datas);
        notifyDataSetChanged();
    }

    public void clearAndAddAll(List<T> datas) {
        mDataList.clear();
        onlyAddAll(datas);
    }

    @Override
    public boolean isSelectedPosition(int position) {
        if (position % 2 == 0) {
            return true;
        }
        return false;
    }
}

3.2 flowtaglayout资源 点击打开链接


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个自定义控件来实现Android网格选择颜色,可以按照以下步骤进行: 1. 创建一个继承自View或其子类的自定义控件类,例如ColorGridView。 2. 在该类中添加必要的属性,例如颜色数组、列数等。 3. 重写onMeasure方法,计算控件的大小。 4. 重写onDraw方法,在控件内绘制颜色格子。 5. 处理触摸事件,当用户点击某个颜色格子时,将该格子的颜色作为选择结果返回。 6. (可选)添加其他交互功能,例如滑动、长按等。 以下是一个简单的实现示例: ``` public class ColorGridView extends View { private int[] colors; private int columnCount; private int selectedColor; public ColorGridView(Context context) { super(context); init(null, 0); } public ColorGridView(Context context, AttributeSet attrs) { super(context, attrs); init(attrs, 0); } public ColorGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs, defStyle); } private void init(AttributeSet attrs, int defStyle) { // 初始化属性 TypedArray a = getContext().obtainStyledAttributes( attrs, R.styleable.ColorGridView, defStyle, 0); // 读取颜色数组 int colorsId = a.getResourceId(R.styleable.ColorGridView_colors, 0); if (colorsId != 0) { TypedArray colorsArray = getResources().obtainTypedArray(colorsId); colors = new int[colorsArray.length()]; for (int i = 0; i < colorsArray.length(); i++) { colors[i] = colorsArray.getColor(i, 0); } colorsArray.recycle(); } // 读取列数 columnCount = a.getInt(R.styleable.ColorGridView_columnCount, 4); a.recycle(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 计算控件大小 int width = MeasureSpec.getSize(widthMeasureSpec); int height = (colors.length / columnCount + 1) * width / columnCount; setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { // 绘制颜色格子 int width = getWidth() / columnCount; int height = width; Paint paint = new Paint(); for (int i = 0; i < colors.length; i++) { int x = (i % columnCount) * width; int y = (i / columnCount) * height; paint.setColor(colors[i]); canvas.drawRect(x, y, x + width, y + height, paint); } } @Override public boolean onTouchEvent(MotionEvent event) { // 处理触摸事件 if (event.getAction() == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); int index = (y / (getWidth() / columnCount)) * columnCount + x / (getWidth() / columnCount); if (index < colors.length) { selectedColor = colors[index]; invalidate(); return true; } } return super.onTouchEvent(event); } public int getSelectedColor() { return selectedColor; } } ``` 在布局文件中可以这样使用: ``` <com.example.ColorGridView android:id="@+id/color_grid_view" android:layout_width="match_parent" android:layout_height="wrap_content" app:colors="@array/colors" app:columnCount="4" /> ``` 其中,colors和columnCount是自定义属性,可以在res/values/attrs.xml文件中定义: ``` <declare-styleable name="ColorGridView"> <attr name="colors" format="reference" /> <attr name="columnCount" format="integer" /> </declare-styleable> ``` 这样,就可以通过ColorGridView控件来实现Android网格选择颜色了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值