Android 购物选择颜色、尺码实现 - Go语言中文社区

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资源 点击打开链接


版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/xiyunmengyuan/article/details/79216276
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-07 22:15:43
  • 阅读 ( 1058 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢