简单的购物车实现

使用HTML简单实现购物车 <head> <meta charset="UTF-8"> <title>我的购物车</title> <style type="text/css"> tr { height: 50px; } </style> <!--第一步引入jq--> <script type="text/j... 阅读详情



本片文章主要是简单实现购物车

所以其中的数据是假数据,其中的图片和选择器都可以替换,只是简单实现一下

使用二级列表实现的 

主要的逻辑就是在适配器


首先看一下数据源GoodBean

package bwie.com.test;

import java.util.List;

public class GoodBean {

    private List<ContentBean> content;
    private int allMoney;
    private int allCount;
    private boolean isAllSelect;

    public boolean isAllSelect() {
        return isAllSelect;
    }

    public void setAllSelect(boolean allSelect) {
        isAllSelect = allSelect;
    }

    public int getAllMoney() {
        return allMoney;
    }

    public void setAllMoney(int allMoney) {
        this.allMoney = allMoney;
    }

    public int getAllCount() {
        return allCount;
    }

    public void setAllCount(int allCount) {
        this.allCount = allCount;
    }

    public List<ContentBean> getContent() {
        return content;
    }

    public void setContent(List<ContentBean> content) {
        this.content = content;
    }

    public static class ContentBean {
        private String id;
        private String address;
        private boolean isSelected;

        private List<GoodDetailBean> goodDetail;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getAddress() {
            return address;
        }

        public void setAdress(String address) {
            this.address = address;
        }

        public boolean isSelected() {
            return isSelected;
        }

        public void setIsSelected(boolean isSelected) {
            this.isSelected = isSelected;
        }

        public List<GoodDetailBean> getGoodDetail() {
            return goodDetail;
        }

        public void setGoodDetail(List<GoodDetailBean> goodDetail) {
            this.goodDetail = goodDetail;
        }

        public static class GoodDetailBean {
            private String id;
            private String pic;
            private String count;
            private String name;
            private String price;
            private boolean isEdit;
            private boolean isSelected;

            public boolean isEdit() {
                return isEdit;
            }

            public boolean isSelected() {
                return isSelected;
            }

            public void setIsSelected(boolean isSelected) {
                this.isSelected = isSelected;
            }

            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public String getPic() {
                return pic;
            }

            public void setPic(String pic) {
                this.pic = pic;
            }

            public String getCount() {
                return count;
            }

            public void setCount(String count) {
                this.count = count;
            }


            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getPrice() {
                return price;
            }

            public void setPrice(String price) {
                this.price = price;
            }

            public boolean isIsedit() {
                return isEdit;
            }

            public void setIsEdit(boolean isEdit) {
                this.isEdit = isEdit;
            }
        }
    }
}


会用到一个接口

package bwie.com.test;

public interface UpdataView {
    void updata(boolean isAllSelected,int count,int price);
}

MainActivity


package bwie.com.test;

import android.app.Activity;
import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class MainActivity extends AppCompatActivity implements UpdataView,View.OnClickListener {
    private ExpandableListView expandableListView;
    private SmoothCheckBox cbSelectAll;
    private TextView moneyAll;
    private TextView tvYunfei;
    private Button butFinish;
    StringBuffer stringBuffer;
    private GoodBean goodBean;
    ExpandableListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
        initAll();

    }

    private void initAll() {
        cbSelectAll.setOnClickListener(this);
    }

    private void initData() {

        AssetManager assetManager = getAssets();
        try {
            InputStream inputStream = assetManager.open("data.json");
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            stringBuffer = new StringBuffer();
            String str;
            while((str = br.readLine()) != null){
                stringBuffer.append(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Gson gson = new Gson();
        goodBean = gson.fromJson(stringBuffer.toString(),GoodBean.class);

        adapter = new ExpandableListAdapter(this,goodBean);
        adapter.setChangedListener(this);
        expandableListView.setAdapter(adapter);
        //展开所有的分组
        for (int i = 0; i < goodBean.getContent().size(); i++) {
            expandableListView.expandGroup(i);
        }
    }

    private void initView() {

        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        cbSelectAll = (SmoothCheckBox) findViewById(R.id.cb_select_all);
        moneyAll = (TextView) findViewById(R.id.money_all);
        tvYunfei = (TextView) findViewById(R.id.tv_yunfei);
        butFinish = (Button) findViewById(R.id.but_finish);

        //去掉二级列表默认的箭头
        expandableListView.setGroupIndicator(null);

        //用于列表滑动时,EditText清除焦点,收起软键盘
        expandableListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                if (SCROLL_STATE_TOUCH_SCROLL == scrollState) {
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity
                            .INPUT_METHOD_SERVICE);
                    View focusView = getCurrentFocus();
                    if (focusView != null) {
                        inputMethodManager.hideSoftInputFromWindow(focusView.getWindowToken(), InputMethodManager
                                .HIDE_NOT_ALWAYS);
                        focusView.clearFocus();
                    }
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });


    }


    @Override
    public void updata(boolean isAllSelected, int count, int price) {
        butFinish.setText("结算:("+count+")");
        moneyAll.setText("¥"+price);
        cbSelectAll.setChecked(isAllSelected);
    }

    @Override
    public void onClick(View v) {
        selectedAll();
    }

    private void selectedAll() {
        int allCount = goodBean.getAllCount();
        int allMoney = goodBean.getAllMoney();
        if (!cbSelectAll.isChecked()) {
            goodBean.setAllSelect(true);
            for (int i = 0; i < goodBean.getContent().size(); i++) {
                goodBean.getContent().get(i).setIsSelected(true);
                for (int n = 0; n < goodBean.getContent().get(i).getGoodDetail().size(); n++) {
                    if (!goodBean.getContent().get(i).getGoodDetail().get(n).isSelected()) {
                        allCount++;
                        allMoney += Integer.valueOf(goodBean.getContent().get(i).getGoodDetail().get(n).getCount())
                                * Integer.valueOf(goodBean.getContent().get(i).getGoodDetail().get(n).getPrice());
                        goodBean.getContent().get(i).getGoodDetail().get(n).setIsSelected(true);
                    }
                }
            }
        } else {
            goodBean.setAllSelect(false);
            for (int i = 0; i < goodBean.getContent().size(); i++) {
                goodBean.getContent().get(i).setIsSelected(false);
                for (int n = 0; n < goodBean.getContent().get(i).getGoodDetail().size(); n++) {
                    goodBean.getContent().get(i).getGoodDetail().get(n).setIsSelected(false);
                }
                allCount = 0;
                allMoney = 0;
            }
        }
        goodBean.setAllMoney(allMoney);
        goodBean.setAllCount(allCount);
        updata(goodBean.isAllSelect(), allCount, allMoney);
        adapter.notifyDataSetChanged();
    }
}


最后是二级列表的适配器

主要的操作是在这里面


package bwie.com.test;

import android.content.Context;
import android.text.Editable;
import android.text.Layout;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private GoodBean goodBean;
    private UpdataView updataViewListener;
    protected static final int KEY_DATA = 0xFFF11133;

    public ExpandableListAdapter(Context context, GoodBean goodBean) {
        this.context = context;
        this.goodBean = goodBean;
    }

    @Override
    public int getGroupCount() {
        return goodBean.getContent().size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return goodBean.getContent().get(groupPosition).getGoodDetail().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return goodBean.getContent().get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return goodBean.getContent().get(groupPosition).getGoodDetail().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_shoppingcart_group, parent, false);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.cbGroupItem.setTag(groupPosition);
        groupViewHolder.cbGroupItem.setOnClickListener(listener);
        groupViewHolder.tvPosition.setText(goodBean.getContent().get(groupPosition).getAddress());
        //根据获取的状态设置是否被选中
        if (goodBean.getContent().get(groupPosition).isSelected()) {
            if (!groupViewHolder.cbGroupItem.isChecked()) {
                groupViewHolder.cbGroupItem.setChecked(true);
            }
        } else {
            groupViewHolder.cbGroupItem.setChecked(false);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_shoppingcart_child, parent, false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        String tag = groupPosition + "," + childPosition;
        childViewHolder.cbItem.setTag(tag);
        childViewHolder.tvReduce.setTag(tag);
        childViewHolder.tvAdd.setTag(tag);
        childViewHolder.imgDelete.setTag(tag);
        childViewHolder.imgIcon.setTag(tag);

        childViewHolder.cbItem.setOnClickListener(listener);
        childViewHolder.tvReduce.setOnClickListener(listener);
        childViewHolder.tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tag = v.getTag().toString();
                String[] split;
                int groupId = 0;
                int childId = 0;
                int allCount = goodBean.getAllCount();
                int allMoney;
                if (tag.contains(",")) {
                    split = tag.split(",");
                    groupId = Integer.parseInt(split[0]);
                    childId = Integer.parseInt(split[1]);
                }
                String goodCount = goodBean.getContent().get(groupId).getGoodDetail().get(childId).getCount();
                goodBean.getContent().get(groupId).getGoodDetail().get(childId).setCount(addCount(goodCount));
                allMoney = goodBean.getAllMoney();
                if (goodBean.getContent().get(groupId).getGoodDetail().get(childId).isSelected()) {
                    allMoney += Integer.valueOf(goodBean.getContent().get(groupId).getGoodDetail().get(childId).getPrice());
                    updataViewListener.updata(goodBean.isAllSelect(), allCount, allMoney);
                }
                goodBean.setAllMoney(allMoney);
                notifyDataSetChanged();
            }
        });

        childViewHolder.imgDelete.setOnClickListener(listener);
        //根据获取的状态设置是否被选中
        if (goodBean.getContent().get(groupPosition).getGoodDetail().get(childPosition).isSelected()) {
            childViewHolder.cbItem.setChecked(true);
        } else {
            childViewHolder.cbItem.setChecked(false);
        }
        //设置数据
        childViewHolder.tvPrice.setText("¥" + goodBean.getContent().get(groupPosition).getGoodDetail().get(childPosition).getPrice());
        childViewHolder.tvGoodName.setText(goodBean.getContent().get(groupPosition).getGoodDetail().get(childPosition).getName());
        //对商品数量的监听
        EditTextWatcher textWatcher = (EditTextWatcher) childViewHolder.etCount.getTag(KEY_DATA);
        if (textWatcher != null) {
            childViewHolder.etCount.removeTextChangedListener(textWatcher);
        }
        childViewHolder.etCount.setText(String.valueOf(goodBean.getContent().get(groupPosition).getGoodDetail().get(childPosition).getCount()));
        EditTextWatcher watcher = new EditTextWatcher(goodBean.getContent().get(groupPosition).getGoodDetail().get(childPosition));
        childViewHolder.etCount.setTag(KEY_DATA, watcher);
        childViewHolder.etCount.addTextChangedListener(watcher);

        childViewHolder.etCount.setText(goodBean.getContent().get(groupPosition).getGoodDetail().get(childPosition).getCount());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

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

            SmoothCheckBox checkBox;
            String tag = v.getTag().toString();
            String[] split;
            int groupId = 0;
            int childId = 0;
            int childSize = 0;
            int groupPosition = 0;
            int allCount = goodBean.getAllCount();//被选中的item数量
            int allMoney = goodBean.getAllMoney();
            if (tag.contains(",")) {
                split = tag.split(",");
                groupId = Integer.parseInt(split[0]);
                childId = Integer.parseInt(split[1]);
            } else {
                groupPosition = Integer.parseInt(tag);
                childSize = goodBean.getContent().get(groupPosition).getGoodDetail().size();
            }
            switch (v.getId()) {
                case R.id.cb_item_group:
                    checkBox = (SmoothCheckBox) v;
                    //根据父checkbox的选中状态设置存储数据里面商品是否被选中
                    goodBean.getContent().get(groupPosition).setIsSelected(!checkBox.isChecked());
                    if (!checkBox.isChecked()) {
                        for (int i = 0; i < childSize; i++) {
                            if (!goodBean.getContent().get(groupPosition).getGoodDetail().get(i).isSelected()) {
                                allCount++;
                                goodBean.getContent().get(groupPosition).getGoodDetail().get(i).setIsSelected(!checkBox.isChecked());
                                allMoney += Integer.valueOf(goodBean.getContent().get(groupPosition).getGoodDetail().get(i).getCount())
                                        * Integer.valueOf(goodBean.getContent().get(groupPosition).getGoodDetail().get(i).getPrice());
                            }
                        }
                    } else {
                        allCount -= childSize;
                        for (int i = 0; i < childSize; i++) {
                            goodBean.getContent().get(groupPosition).getGoodDetail().get(i).setIsSelected(!checkBox.isChecked());
                            allMoney -= Integer.valueOf(goodBean.getContent().get(groupPosition).getGoodDetail().get(i).getCount())
                                    * Integer.valueOf(goodBean.getContent().get(groupPosition).getGoodDetail().get(i).getPrice());
                        }
                    }
                    //父item选中的数量
                    int fCount = 0;
                    //判断是否所有的父item都被选中,决定全选按钮状态
                    for (int i = 0; i < goodBean.getContent().size(); i++) {
                        if (goodBean.getContent().get(i).isSelected()) {
                            fCount++;
                        }
                    }
                    if (fCount == goodBean.getContent().size()) {
                        goodBean.setAllSelect(true);
                    } else {
                        goodBean.setAllSelect(false);
                    }
                    goodBean.setAllCount(allCount);
                    goodBean.setAllMoney(allMoney);
                    notifyDataSetChanged();
                    updataViewListener.updata(goodBean.isAllSelect(), allCount, allMoney);
                    break;
                //单个子项item被点击
                case R.id.cb_item:
                    checkBox = (SmoothCheckBox) v;
                    int cCount = 0;//子item被选中的数量
                    int fcCount = 0;//父item被选中的数量
                    goodBean.getContent().get(groupId).getGoodDetail().get(childId).setIsSelected(!checkBox.isChecked());
                    //遍历父item所有数据,统计被选中的item数量
                    for (int i = 0; i < goodBean.getContent().get(groupId).getGoodDetail().size(); i++) {
                        if (goodBean.getContent().get(groupId).getGoodDetail().get(i).isSelected()) {
                            cCount++;
                        }
                    }
                    //判断是否所有的子item都被选中,决定父item状态
                    if (cCount == goodBean.getContent().get(groupId).getGoodDetail().size()) {
                        goodBean.getContent().get(groupId).setIsSelected(true);
                    } else {
                        goodBean.getContent().get(groupId).setIsSelected(false);
                    }
                    //判断是否所有的父item都被选中,决定全选按钮状态
                    for (int i = 0; i < goodBean.getContent().size(); i++) {
                        if (goodBean.getContent().get(i).isSelected()) {
                            fcCount++;
                        }
                    }
                    if (fcCount == goodBean.getContent().size()) {
                        goodBean.setAllSelect(true);
                    } else {
                        goodBean.setAllSelect(false);
                    }
                    //判断子item状态,更新结算总商品数和合计Money
                    if (!checkBox.isChecked()) {
                        allCount++;
                        allMoney += Integer.valueOf(goodBean.getContent().get(groupId).getGoodDetail().get(childId).getCount())
                                * Integer.valueOf(goodBean.getContent().get(groupId).getGoodDetail().get(childId).getPrice());
                    } else {
                        allCount--;
                        allMoney -= Integer.valueOf(goodBean.getContent().get(groupId).getGoodDetail().get(childId).getCount())
                                * Integer.valueOf(goodBean.getContent().get(groupId).getGoodDetail().get(childId).getPrice());
                    }
                    goodBean.setAllCount(allCount);
                    goodBean.setAllMoney(allMoney);
                    notifyDataSetChanged();
                    updataViewListener.updata(goodBean.isAllSelect(), allCount, allMoney);
                    break;
                case R.id.tv_reduce:
                    //减少商品数量
                    String goodCount = goodBean.getContent().get(groupId).getGoodDetail().get(childId).getCount();
                    if (Integer.valueOf(goodCount) > 1) {
                        goodBean.getContent().get(groupId).getGoodDetail().get(childId).setCount(reduceCount(goodCount));
                        if (goodBean.getContent().get(groupId).getGoodDetail().get(childId).isSelected()) {
                            allMoney -= Integer.valueOf(goodBean.getContent().get(groupId).getGoodDetail().get(childId).getPrice());
                            updataViewListener.updata(goodBean.isAllSelect(), allCount, allMoney);
                        }
                        goodBean.setAllMoney(allMoney);
                        notifyDataSetChanged();
                    }
                    break;
                case R.id.img_delete:
                    goodBean.getContent().get(groupId).getGoodDetail().remove(childId);
                    if (goodBean.getContent().get(groupId).getGoodDetail().size() == 0) {
                        goodBean.getContent().remove(groupId);
                    }
                    notifyDataSetChanged();
                    break;
            }
        }
    };


    public void setChangedListener(UpdataView listener) {
        if (updataViewListener == null) {
            this.updataViewListener = listener;
        }
    }

    private String addCount(String var) {
        Integer integer = Integer.valueOf(var);
        integer++;
        return integer + "";
    }

    private String reduceCount(String var) {
        Integer integer = Integer.valueOf(var);
        if (integer > 1) {
            integer--;
        }
        return integer + "";
    }

    /**
     * 商品数量EditText内容改变的监听
     */
    class EditTextWatcher implements TextWatcher {

        private GoodBean.ContentBean.GoodDetailBean GoodDetail;

        public EditTextWatcher(GoodBean.ContentBean.GoodDetailBean item) {
            this.GoodDetail = item;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(s.toString().trim())) {
                String textNum = s.toString().trim();
                GoodDetail.setCount(textNum);
            }
        }
    }

    static class GroupViewHolder {

        SmoothCheckBox cbGroupItem;
        TextView tvPosition;

        GroupViewHolder(View view) {
            cbGroupItem = view.findViewById(R.id.cb_item_group);
            tvPosition = view.findViewById(R.id.tv_position);
        }
    }

    static class ChildViewHolder {
        SmoothCheckBox cbItem;
        TextView tvPrice, tvGoodName;
        EditText etCount;
        TextView tvReduce;
        TextView tvAdd;
        ImageView imgDelete;
        ImageView imgIcon;

        ChildViewHolder(View view) {
            cbItem = (SmoothCheckBox) view.findViewById(R.id.cb_item);
            tvPrice = (TextView) view.findViewById(R.id.tv_price);
            tvGoodName = (TextView) view.findViewById(R.id.tv_good_name);
            etCount = (EditText) view.findViewById(R.id.et_count);
            tvReduce = (TextView) view.findViewById(R.id.tv_reduce);
            tvAdd = (TextView) view.findViewById(R.id.tv_add);
            imgDelete = (ImageView) view.findViewById(R.id.img_delete);
            imgIcon = (ImageView) view.findViewById(R.id.img_icon);
        }
    }

}


activity_main

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

    <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:divider="@null"
        android:scrollbars="none" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/divideLine" />


    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical">

        <bwie.com.test.SmoothCheckBox
            android:id="@+id/cb_select_all"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginLeft="10dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="@string/select_all"
            android:textColor="#333" />

        <RelativeLayout
            android:id="@+id/rel2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <RelativeLayout
                android:layout_marginTop="5dp"
                android:id="@+id/rel"
                android:layout_marginRight="100dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true">

                <TextView
                    android:id="@+id/heji"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="合计"
                    android:textColor="#333"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/money_all"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/heji"
                    android:text="¥0"
                    android:textColor="#fe3824" />

            </RelativeLayout>

            <TextView
                android:id="@+id/tv_yunfei"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="100dp"
                android:layout_below="@id/rel"
                android:text="运费:¥0" />


            <Button
                android:id="@+id/but_finish"
                android:layout_width="95dp"
                android:text="结算(0)"
                android:background="#fe3824"
                android:layout_centerVertical="true"
                android:layout_alignParentRight="true"
                android:layout_height="wrap_content" />
        </RelativeLayout>


    </LinearLayout>


</LinearLayout>

item_shoppingcart_group

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


    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/divideLine"
        />

    <LinearLayout
        android:padding="15dp"
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
        <bwie.com.test.SmoothCheckBox
            android:id="@+id/cb_item_group"
            android:layout_width="25dp"
            android:layout_height="25dp" />

        <TextView
            android:id="@+id/tv_position"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:gravity="center_vertical"
            android:layout_weight="1"
            android:drawableLeft="@drawable/ic_position"
            android:drawablePadding="3dp"
            android:text="京东旗舰店发货"
            android:textColor="#333333"
            android:textSize="15sp" />


        
    </LinearLayout>

</LinearLayout>

item_shoppingcart_child

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:paddingBottom="15dp"
    android:paddingRight="15dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/ll_check"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:paddingLeft="15dp"
        android:paddingRight="17dp">

        <bwie.com.test.SmoothCheckBox
            android:id="@+id/cb_item"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_gravity="center_vertical" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/divideLine" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/img_icon"
                android:layout_width="78dp"
                android:layout_height="78dp"
                android:src="@drawable/ic_phone" />

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1">

                <TextView
                    android:id="@+id/tv_good_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="KKKKKKK" />

                <TextView
                    android:id="@+id/tv_reduce"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_below="@id/tv_good_name"
                    android:layout_marginTop="6dp"
                    android:background="@drawable/selector_shopping_cart_subtract"
                    android:gravity="center"
                    android:text="-"
                    android:textColor="@color/text_666666"
                    android:textSize="15sp" />

                <EditText
                    android:id="@+id/et_count"
                    android:layout_width="49dp"
                    android:layout_height="30dp"
                    android:layout_alignTop="@+id/tv_reduce"
                    android:layout_marginBottom="1dp"
                    android:layout_toRightOf="@+id/tv_reduce"
                    android:background="@drawable/bg_input_box"
                    android:gravity="center"
                    android:inputType="number"
                    android:maxLength="6"
                    android:text="1"
                    android:textColor="@color/text_666666"
                    android:textCursorDrawable="@null"
                    android:textSize="12sp" />

                <TextView
                    android:id="@+id/tv_add"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_alignTop="@+id/tv_reduce"
                    android:layout_toRightOf="@id/et_count"
                    android:background="@drawable/selector_shopping_cart_add"
                    android:gravity="center"
                    android:text="+"
                    android:textColor="@color/text_666666"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/tv_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:text="¥899"
                    android:textColor="#FE3824"
                    android:textSize="13sp" />
            </RelativeLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="18dp"
                android:gravity="end"
                android:orientation="vertical">

            </LinearLayout>

            <ImageView
                android:id="@+id/img_delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginTop="5dp"
                android:src="@drawable/icon_delete" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>





JS实现简单的购物车 中是否已经有该产品,有的话数量加一并更新,没有则创建一个新对象并添加到购物车列表中。同时,每次添加或删除购物车物品后都需要更新购物车总计。最后,点击结算按钮后将购物车清空并重置总金额。作为购物车中的物品,它的键是产品 ID,值是包含产品信息的对象。添加至购物车时,首先检查。由于是实现基本的功能,就不弄得多复杂了,代码可以直接Ctrl+c/v,大家可以试一试!这个简单的购物车实现使用了一个对象。 阅读详情

相关推荐

AngularJS 实现简单购物车

先看看界面:         我的购物车 //这里是自己的angular.js的路径                 * {             margin: 0;             padding: 0;         }         table {             border-collapse: col

潇湘夜雨 1799

SmoothCheckBox -- 源码分析

前言 在项目中使用到了 github 上的开源项目 SmoothCheckBox,觉得值得学习。 大胆猜测 从动画效果可以看出来,效果该分为三个部分: 一:对勾 是一部分, 这应该是使用了 drawLine 的相关API,难点在于怎么动态的画出对勾。 二:内容的填充色 ,这一部分,该是 drawCircle 的相关API,难点则在于平滑的重绘。 三:最后,border 也是单独

jiahuan 2355

React实现简单购物车

&amp;lt;!DOCTYPE html&amp;gt; &amp;lt;html&amp;gt; &amp;lt;head&amp;gt; &amp;lt;meta charset=&quot;UTF-8&quot;&amp;gt; &amp;lt;title&amp;gt;&amp;lt;/title&amp;gt; &amp;lt;script src=&qu

qq_36276469的博客 3648

推荐开源项目:SmoothCheckBox - 演绎平滑过渡的Checkbox动画效果

推荐开源项目:SmoothCheckBox - 演绎平滑过渡的Checkbox动画效果 在前端开发中,动效设计往往能提升用户体验,使应用更加生动有趣。今天我们要介绍的开源项目——,正是这样一个能够为你的Checkbox带来流畅、自然过渡效果的小巧工具。 项目简介 SmoothCheckBox 是一个基于Android平台的自定义 Checkbox 控件,它利用了Android的绘图机制,实现了一种...

gitblog_00072的博客 508

js实现简单购物车

先上样式 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title&

m0_52716501的博客 2110

自己写的假数据静态购物网小程序

写的完的用了很多的样式保存着以后需要的话留着参考

Android-带有切换动画的CheckBox

转自:https://github.com/andyxialm/SmoothCheckBox前言切换带动画的效果确实不错,可能以后项目会用得到,所以转发收藏。1,自定义 SmoothCheckBox 。public class SmoothCheckBox extends View implements Checkable { private static final String KEY_

七号座先生的博客 6018

Android代码-SmoothCheckBox

SmoothCheckBox ScreenShot 截图: Attrs 属性 attr format description duration integer 动画持续时间 stroke_width dimension 未选中时边框宽度 color_tick color 对勾颜色 color_checked color 选中时填充颜色 color_unchecked color 未选中时填充颜色 color_unchecked_stroke color 未选中时边框颜色 Sample Usage 使用 setChecked(boolean checked); // 默认不带动画,若需要动画 调用重载方法 setChecked(boolean checked, boolean animate); // 参数: animate 是否显示动画 protected void onCreate(Bundle savedInstanceState) { super.onCre

uniapp项目简单实现购物车

一个电商项目中可以说购物车功能的实现可以说是非常重要的,这次疫情期间刚好自己做了一个小demo,停工不停学,逆战到天明。话不多说,先看看效果。 一、当我们进入购物车页面时应该首先判断用户是不是出于登录状态 onShow() { try { const userid = uni.getStorageSync('userid'); const token = uni.getStorag...

小王的技术博客 1万+

简易实现购物车.zip

HTML5新手小案例,实现简单的购物车功能,新手代码,老鸟请跳过,简单易懂,没有过多的复杂的CSS,HTML5新手请笑纳。

简单实现购物车

模拟实现一个简单的购物车 加入结算 数量 总计 结算

javaweb简单实现购物车的功能

功能简述 本例采用jsp+servlet+mysql+jdbc+c3p0+ajax简单实现一个购物车的功能。首先会进入购物页面,每个商品

陈常杰的博客 2万+

Smooth Checkbox for Hotmail:trade_mark:-crx插件

语言:English 通过单击和拖动,选择/取消选择多个复选框! 现在您不必在选择多个复选框时单击每个复选框。 您只需单击并拖动以选择一束。 取消选择行为: 如果在取消选择期间拖动光标,将取消选择每个复选框。

html简易的购物车,JavaScript实现简单的购物车

最近刚好在了解web storage的知识,恰巧被朋友问及有没有做过购物车的功能,想了一下还真没有,然后试着写了这么一个例子。只是单纯的一个页面实现了购物车逻辑上的一些简单的功能:添加商品,以及购物车列表动态添加、删除,并实现商品总价格的更新。功能可能确实简单,但我还是将它分享出来,也算是对自己学习的一种鼓励。慢慢积累,终究也能成为大牛,加油下面是效果图:下面源代码:huanglw笔记之web s...

weixin_39915308的博客 1133

《活用数据——驱动业务的数分析实战》读书笔记 (实战篇)

第四章、战略分析案例解析         如思维篇所述,分析思路是从研究目的到研究内容的分解过程。因此需要考虑如下两个问题: 战略分析的研究目的 战略分析的研究内容 1、研究目的:战略选择         战略分析的研究目的是帮助企业进行战略选择,选择适合自己的目标市场。需要回答以下两个问题: 市场吸引力:这个市...

Xiaozhu的博客 3698

Nsight Compute内存图表实战:如何快速定位CUDA内核的内存瓶颈(附配置截图)

本文详细介绍了如何使用Nsight Compute的Memory Chart功能快速定位和诊断CUDA内核中的内存性能瓶颈。通过解析内存图表中的逻辑与物理单元节点、连线颜色含义,并结合实战案例(如低L1缓存命中率、共享内存Bank冲突、全局内存访问未合并),指导开发者从可视化数据回溯至源代码,进行精准有效的内存访问优化,从而提升GPU程序性能。

zeta9的博客 479

Python实现简单的购物车

思路: 1.引导用户输入金额 2.引导用户选择商品 3.根据用户的选择将商品添加到购物车 4.删除商品 5.结算购物车,退出系统 # 购物车 # 商品名称作为key,商品数量作为value shoppingcar = {} # 添加商品 def addgoods(product, num): if num.isdigit(): # isdigit()如果字符串只包含数字则返回 True 否则返回 False。 num = int......

Mogul的博客 8064

OK3588-C开发板Forlinx Desktop系统烧录与多媒体功能实战

本文详细介绍了OK3588-C开发板Forlinx Desktop系统的烧录步骤与多媒体功能实战。从硬件准备到软件工具配置,逐步解析RKDevTool烧录技巧,并深入测试摄像头、视频编解码及音频功能,帮助开发者快速掌握RK3588芯片的强大性能与Ubuntu系统的优化应用。

weixin_29190169的博客 417

php 实现简单加入购物车

这个购物车相对来说比较简单,用于短暂存储,并没有存储到数据库,购物车对于爱网购的人来说简直是熟悉的不能再熟悉了,在写购物车之前,我们首先要构思一下,我们需要先从数据库中调出一张表格,这里我用的是fruit表,其次是登录表,我用的是login表,用来调用户名和密码的,所有的都准备好之后就要考虑放入购物车是会有三种情况的: 第一种情况:购物车里面什么都没有 第二种情况:购物

小孩的博客 1万+

使用单片机和电流互感器对非正弦周期电流有效值测定

使用单片机和电流互感器,利用一种高速采样的方法来进行有效值测定

月光绿叶滴水音乐 3944

YOLOv11 改进策略 | HWD:引入 Haar 小波变换,减少信息丢失

在卷积神经网络(CNN)中,下采样(Downsampling)是降低特征图分辨率、扩大感受野的关键操作,通常通过最大池化(MaxPool)或步长为 2 的卷积(Strided Convolution)实现。然而,这些方法在降低分辨率的同时,往往会丢失图像中的高频细节信息(如边缘、纹理)和精确的空间位置信息,这可能对后续特征提取和目标检测精度产生不利影响,尤其是对于小目标和需要精细定位的任务。

走向CTO的路上... 1789

Vue简单实现购物车全部功能

Vue简单实现购物车功能 本文用Vue简单实现了一下购物车功能,简单是指代码简单,只有50行,也指样式简单,愧对前端。话不多说,直接上图上代码吧! 这是购物车的页面,虽然看着比较简陋,但麻雀虽小五脏俱全,数据用的是在state里面的固定数据。 先捋一下购物车的逻辑: 1、在选中该商品的时候,总价变为该商品的价格。 2、点击”+“或 ” - “的时候,商品的数量随之变化,总价也跟着变化。 3、当点击全选的时候,全部商品选中,总价变为如今商品价格*商品数量之和,取消全选的时候,价格变为0。 4

qing_xing_的博客 1万+

Javaweb MVC+dao实现简单购物车(实验)

Javaweb MVC+dao实现简单购物车。本篇文章给大家分享一下简单购物车的实现。含商品图片、清空购物车、指定数量增加、结算。

poem23的博客 2996

EMC中的RE测试与整改

测试主要测试电子、电气设备或系统在正常工作时自身对外界的辐射干扰强度,包括来自电路板、机箱、电缆及连接线等所有部件的辐射骚扰。分为:磁场辐射和。其中后者较为应用普遍。测试频段根据不同标准要求不一致。手机测试的频段主要为30-1000MHZ。需要注意的是设备在进行RE测试时要求尽可能满配置,满负荷的运行。同时RE也是EMC中的难点。

学海无涯的专栏 1670

在 SAP BTP 中玩转工厂日历 Maintain Factory Calendars 实战指南

工厂日历是企业关键的时间主数据,基于国家假日历定义工作日与非工作日,广泛应用于生产计划、采购交期、财务结账等业务场景。SAP系统通过三类应用(维护节假日、假日历和工厂日历)实现分层配置。工厂日历作为顶层配置,支持工作日模式设定和例外规则添加。在多系统架构中,工厂日历可通过集成场景同步,确保数据一致性。应用支持创建、编辑和查看功能,包含有效期设置、例外规则管理和多语言文本维护。合理设置工厂日历对业务运营至关重要,需注意命名规范和有效期选择,避免历史数据问题。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 222
上一篇: 单位转换,尺寸不变
下一篇: 简易搜索页面
Melect
博客等级 码龄9年 10粉丝 46原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值