购物车界面(Bootstrap)

这篇博客探讨了购物车界面的实现,重点在于如何使用cookie或session保存用户选择的商品,以及购物车的功能,包括添加、删除商品,修改数量和计算总价。博主详细介绍了购物流程,并提供了相关代码块,如expandable适配器和自定义加减控件。

分析:

          1、网络购物车是一个虚拟的购物车,但是要实现像真实购物一样的大概流程,

添加商品,丢掉商品,结算价钱等等

1、 因为只有用户确定购买的商品才是有效的,并且选择不同的商品要浏览不同的页面,所以需要长久保存数据,但是还不能把所有的选过的商品都添加到数据库中,这样如果客户不要该商品时还需要在数据库中删除增加了服务器的压力,所以要筛选到只有客户最终确定的商品才添加到数据库进行保存,这时就需要一个临时的全局变量来保存数据,cookie 或者是 session 他们的优缺点以下介绍:

2、

3、 功能:

a) 商品展示,相关资料(基本描述,价格,名称,供应商情况,图片等)

b) 购物车功能

                   i.             用户可以再购物王章不同页面之间任意跳转,选购自己喜欢的商品---点击购买----订单信息----统一到付账台结账

功能包括:

1) 添加商品—-订购

2) 删除商品

3) 修改商品的购买数量(小计,总价)

4) 清空购物车

ii:订单

   详细信息表单

   支付方式----支付宝接口

购物流程:商品浏览—-加入购物车—–购物结算——生成订单——提交订单

代码块

main主页
public class ShoppingCar extends Fragment implements IGwCheView, View.OnClickListener {
    private List<GoodsBean.DataBean> list;
    private MyAdapter_car adapter;
    private View view;
    private ExpandableListView mMyexpandle;
    /**
     * 全选/反选
     */
    private CheckBox mCbQxMain;
    /**
     * 合计:0
     */
    private TextView mTvSum;
    /**
     * 去结算(0)
     */
    private Button mJisuan;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.shoppcar, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        int uid = 12007;

        PresenterImp presenterImp = new PresenterImp();
        presenterImp.showShoppingCarToView(new ModleImp(), getActivity(), this,uid);

    }

    @Override
    public void onResume() {
        super.onResume();
        int uid = 12007;

        PresenterImp presenterImp = new PresenterImp();
        presenterImp.showShoppingCarToView(new ModleImp(), getActivity(), this,uid);
    }



    @Override
    public void showGowWuList(List<GoodsBean.DataBean> data) {
        Log.d("GwCheFragment----------", "data:" + data);

        list = data;

        adapter = new MyAdapter_car(data, getActivity(), this);
        mMyexpandle.setAdapter(adapter);
//        打开二级列表
        int count = mMyexpandle.getCount();
        for (int i = 0; i < count; i++) {
            mMyexpandle.expandGroup(i);
        }
//        分割线
        mMyexpandle.setDivider(getResources().getDrawable(R.drawable.shap_param));
        mMyexpandle.setChildDivider(getResources().getDrawable(R.drawable.shap_child));
    }

//    显示总价
    @Override
    public void showCount(String count) {
        mTvSum.setText("总价:"+count);
    }

    private void initView(View view) {
        mMyexpandle = (ExpandableListView) view.findViewById(R.id.myexpandle);
        mCbQxMain = (CheckBox) view.findViewById(R.id.cb_qx_main);
        mTvSum = (TextView) view.findViewById(R.id.tv_sum);
        mJisuan = (Button) view.findViewById(R.id.jisuan);
        mJisuan.setOnClickListener(this);
        mCbQxMain.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
//                全选   获取bean中的多选框状态值,赋予状态
            case R.id.cb_qx_main:
                boolean checked = mCbQxMain.isChecked();
                //只需要改变集合里面的数据,就可以了,然后刷新
                for (int i = 0; i < list.size(); i++) {
                    GoodsBean.DataBean dataBean = list.get(i);
                    dataBean.setParentIsSelected(checked);
                    //获得子集合,遍历
                    List<GoodsBean.ChildBean> list1 = dataBean.getList();
                    for (int j = 0; j < list1.size(); j++) {
                        list1.get(j).setChildIsSelect(checked);
                    }

                }
                //刷新即可
                adapter.notifyDataSetChanged();

                break;
            case R.id.jisuan:

                Toast.makeText(getActivity(), "去结算------", Toast.LENGTH_SHORT).show();

                break;
        }
    }
}

布局界面-----------------------------------------

<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ExpandableListView
        android:id="@+id/myexpandle"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:groupIndicator="@null"
        android:layout_weight="1">

    </ExpandableListView>


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

        <CheckBox
            android:id="@+id/cb_qx_main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选/反选"
            android:layout_marginRight="10dp"/>

        <TextView
            android:id="@+id/tv_sum"
            android:layout_width="0dp"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="合计:0" />

        <Button
            android:id="@+id/jisuan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#F00"
            android:text="去结算(0)" />
    </LinearLayout>

</LinearLayout>

expandable适配器

public class MyAdapter_car extends BaseExpandableListAdapter{
    List<GoodsBean.DataBean> data;
    Context context;
    IGwCheView iGwCheView;
    private ViewHolder holder;
    private ViewHolder1 holder1;

    public MyAdapter_car(List<GoodsBean.DataBean> data, Context context, IGwCheView iGwCheView) {
        this.data = data;
        this.context = context;
        this.iGwCheView = iGwCheView;
    }
    //获取父分组的个数
    @Override
    public int getGroupCount() {
        return data.size();
    }
    //获取子分组的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return data.get(groupPosition).getList().size();
    }
    //获取父分组的对象
    @Override
    public Object getGroup(int groupPosition) {
        return data.get(groupPosition);
    }
    //获取子分组的对象
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.get(groupPosition).getList().get(childPosition);
    }
    // //获取父分组的id
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

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

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

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
//优化赋值
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.layout_parent, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mParentTitle.setText(data.get(groupPosition).getSellerName());
        holder.mParentCb.setChecked(data.get(groupPosition).isParentIsSelected());
        //商家单选框
        holder.mParentCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                boolean selected = data.get(groupPosition).isParentIsSelected();
                selected = !selected;
                data.get(groupPosition).setParentIsSelected(selected);
                //点击父条目的复选框,子条目,相应的改变
                //将复选框的状态赋值给子复选框
                List<GoodsBean.ChildBean> list = MyAdapter_car.this.data.get(groupPosition).getList();
                for (int i = 0; i < list.size(); i++) {
                    GoodsBean.ChildBean childBean = list.get(i);
                    childBean.setChildIsSelect(selected);
                }
                notifyDataSetChanged();
            }
        });
//计算
        String sum = SumUtils.sum(data);
        iGwCheView.showCount(sum);

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
//优化赋值
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.layout_child, null);
            holder1 = new ViewHolder1(convertView);
            convertView.setTag(holder1);
        } else {
            holder1 = (ViewHolder1) convertView.getTag();
        }

        List<GoodsBean.ChildBean> childBeanList = MyAdapter_car.this.data.get(groupPosition).getList();

        holder1.mChildPrice.setText(childBeanList.get(childPosition).getPrice() + "¥");
        holder1.mChildTitle.setText(childBeanList.get(childPosition).getTitle());

//图片赋值
        String images = childBeanList.get(childPosition).getImages();
        String pic_url = images.split("\\|")[0];
        Glide.with(context).load(pic_url).into(holder1.mChildPic);
//        设置初始化商品数值
        num = data.get(groupPosition).getList().get(childPosition).getNum();
        holder1.mAddSubView.setCount(num);
//        单选框状态赋值

        holder1.mChildCb.setChecked(data.get(groupPosition).getList().get(childPosition).isChildIsSelect());
//       加减条目赋值
        holder1.addSubView.setCount(data.get(groupPosition).getList().get(childPosition).getNum());

        holder1.mChildCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean select = data.get(groupPosition).getList().get(childPosition).isChildIsSelect();
                select = !select;
                data.get(groupPosition).getList().get(childPosition).setChildIsSelect(select);
                //当所有的字条目被选中,父条目也要被选中。如果有一个字条目没有选中,父条目就不能选中
                List<GoodsBean.ChildBean> list = MyAdapter_car.this.data.get(groupPosition).getList();
                boolean flag = true;
                for (int i = 0; i < list.size(); i++) {
                    if (!list.get(i).isChildIsSelect()) {
                        flag = false;
                    }
                }

                data.get(groupPosition).setParentIsSelected(flag);
                notifyDataSetChanged();

                //计算
                String sum = SumUtils.sum(data);
                iGwCheView.showCount(sum);

            }
        });


        //减条目
        holder1.addSubView.setOnSubClickListener(new AddSubView.SubClickListener() {
            @Override
            public void onSubClick(View view, int count) {
//                获取子条目中减条目  设置数值
                data.get(groupPosition).getList().get(childPosition).setNum(count);
                boolean select = data.get(groupPosition).getList().get(childPosition).isChildIsSelect();
//                如果复选框被选中就计算价钱
                if (select) {
                    String sum = SumUtils.sum(data);
                    iGwCheView.showCount(sum);
                }
            }
        });
        //加条目
        holder1.addSubView.setOnAddClickListener(new AddSubView.AddClickListener() {
            @Override
            public void onAddClick(View view, int count) {
//                获取子条目中加条目  设置数值
                data.get(groupPosition).getList().get(childPosition).setNum(count);
                boolean select = data.get(groupPosition).getList().get(childPosition).isChildIsSelect();
//                如果复选框被选中就计算价钱
                if (select) {
                    String sum = SumUtils.sum(data);
                    iGwCheView.showCount(sum);
                }

            }
        });

//删除
        holder1.delet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data.get(groupPosition).getList().remove(childPosition);
                data.remove(groupPosition);
                notifyDataSetChanged();
            }
        });


        return convertView;
    }
    //子条目可以点击
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        View view;
        CheckBox mParentCb;
        TextView mParentTitle;

        ViewHolder(View view) {
            super(view);
            this.view = view;
            this.mParentCb = view.findViewById(R.id.parent_cb);
            this.mParentTitle = view.findViewById(R.id.parent_title);
        }
    }

    class ViewHolder1 {
        View view;
        CheckBox mChildCb;
        ImageView mChildPic;
        TextView mChildTitle;
        TextView mChildPrice;
        AddSubView addSubView;
         Button delet;

        ViewHolder1(View view) {
            this.view = view;
            this.mChildCb = view.findViewById(R.id.child_cb);
            this.mChildPic = view.findViewById(R.id.child_pic);
            this.mChildTitle = view.findViewById(R.id.child_title);
            this.mChildPrice = view.findViewById(R.id.child_price);
            this.addSubView = view.findViewById(R.id.add_sub_view);
            this.delet = view.findViewById(R.id.delet);
        }
    }
}


父布局---------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:id="@+id/parent_cb"
        style="@style/Widget.AppCompat.CompoundButton.RadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/parent_title"
        android:text="商家"
        android:singleLine="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

子布局---------------------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/child_cb"
        style="@style/Widget.AppCompat.CompoundButton.RadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/child_pic"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher" />

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

        <TextView
            android:singleLine="true"
            android:id="@+id/child_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="商品名"
            android:textSize="25sp" />
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <TextView
                android:singleLine="true"
                android:id="@+id/child_price"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="商品价格"
                android:textColor="#F00" />
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content" />

            <com.example.dell.monthpractice.CustomView.AddSubView
                android:id="@+id/add_sub_view"
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="wrap_content">

            </com.example.dell.monthpractice.CustomView.AddSubView>

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content" />

            <Button
                android:id="@+id/delet"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="40dp"
                android:text="删除"
                android:background="#f00"/>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

自定义加减控件———————————————–

public class AddSubView extends LinearLayout implements View.OnClickListener {
    private TextView sub;
    private TextView add;
    private EditText count;
    private AddClickListener addClickListener;
    private SubClickListener subClickListener;

    public AddSubView(Context context) {
        this(context, null);
    }

    public AddSubView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        View view = View.inflate(context, R.layout.add_sub_layout, this);

        //获取控件
        sub = view.findViewById(R.id.sub);
        add = view.findViewById(R.id.add);
        count = view.findViewById(R.id.count);
        sub.setOnClickListener(this);
        add.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sub:
                Log.d("AddSubView-----", "点击-号");
//                获取Editext值,如果大于0,就减减数值
                String s = count.getText().toString();
                int i = Integer.parseInt(s);
                if (i <= 0) {
                    return;
                }
                count.setText(--i + "");
                Log.d("AddSubView-------+--", "-----i:" + i);
                subClickListener.onSubClick(v, i);

                break;
            case R.id.add:
                Log.d("AddSubView-----", "点击+号");
//                获取Editext值,如果大于0,就加价数值
                String s1 = count.getText().toString();
                int i1 = Integer.parseInt(s1);
                count.setText(++i1 + "");
                Log.d("AddSubView++++++++++", "-----i:" + i1);
                addClickListener.onAddClick(v, i1);
                break;


        }
    }

    public int getCount() {
        return Integer.parseInt(count.getText().toString().trim());
    }

    public void setCount(int s) {
        count.setText(s + "");
    }

//内部监听----加数值
    public interface AddClickListener {
        void onAddClick(View view, int count);
    }
//外部调用----加数值
    public void setOnAddClickListener(AddClickListener addClickListener) {
        this.addClickListener = addClickListener;
    }
    //内部监听----减数值
    public interface SubClickListener {
        void onSubClick(View view, int count);
    }
    //外部调用----减数值
    public void setOnSubClickListener(SubClickListener subClickListener) {
        this.subClickListener = subClickListener;
    }

}

自定义加减布局--------------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="30dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/sub"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/add_sub_shape"
        android:gravity="center"
        android:text="-"
        android:textSize="25sp" />

    <EditText
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/add_sub_shape"
        android:focusable="false"
        android:gravity="center"
        android:text="0" />

    <TextView
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/add_sub_shape"

        android:gravity="center"
        android:text="+"
        android:textSize="25sp" />

</LinearLayout>

资源文件

add_sub_shape
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size
        android:width="100dp"
        android:height="30dp" />
    <stroke
        android:width="1dp"
        android:color="#999999" />

</shape>

添加bean中复选框状态值

父控件状态值:

     private boolean parentIsSelected;

        public boolean isParentIsSelected() {
            return parentIsSelected;
        }

        public void setParentIsSelected(boolean parentIsSelected) {
            this.parentIsSelected = parentIsSelected;
        }

子控件状态值:

    //是否选中的状态
private boolean ChildIsSelect;


        public boolean isChildIsSelect() {
            return ChildIsSelect;
        }

        public void setChildIsSelect(boolean childIsSelect) {
            ChildIsSelect = childIsSelect;
        }

工具类–计算总价

/**
 * 工具类---计算总和
 */

public class SumUtils {

    public static String sum(List<GoodsBean.DataBean> data){
        double sum=0;
        for (int i = 0; i < data.size(); i++) {

            List<GoodsBean.ChildBean> list = data.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                GoodsBean.ChildBean childBean = list.get(j);
//                如果被选中就累加价钱
                if (childBean.isChildIsSelect()){
                    sum+=childBean.getPrice()*childBean.getNum();
                }
            }

        }

        return Double.toString(sum);
    }

}
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值