Android 购物车的简单实现

逻辑

1.实体类添加属性:isSelect 是否选中
2.适配器根据isSelect判断是否选中+复选框点击
3.全选和取消全选:修改集合中所有的数据源的isSelect—>刷新适配器---->计算总价
4.用户反选:修改当前item的数据源isSelect—>刷新适配器----》计算总价



使用步骤

1.引入库

代码如下(示例):

implementation 'com.lzy.net:okgo:3.0.4'//okgo依赖
implementation 'com.google.code.gson:gson:2.8.0'//gson依赖
implementation 'com.github.bumptech.glide:glide:4.9.0'//glide依赖
implementation 'com.lzy.net:okgo:3.0.4'//okgo依赖
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'//万能适配器
implementation 'com.android.support:recyclerview-v7:30.0.0'//recyclerview

manifests清单文件记得加网络权限
 <uses-permission android:name="android.permission.INTERNET" />

项目gradle:
allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }//万能适配器
    }
}

2.bean类

// 直接get请求 GsonFormat
http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1

****** 加一个是否选中判断
// todo 是否选中
private boolean isSelect;
然后 //get set

public class XiaBean {
    /**
     * ret : 1
     * data : [{"id":"8289","title":"油焖大虾","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg","collect_num":"1670","food_str":"大虾 葱 生姜 植物油 料酒","num":1670},{"id":"2127","title":"四川回锅肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg","collect_num":"1591","food_str":"猪肉 青蒜 青椒 红椒 姜片","num":1591},{"id":"30630","title":"超简单芒果布丁","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg","collect_num":"1549","food_str":"QQ糖 牛奶 芒果","num":1549},{"id":"9073","title":"家常红烧鱼","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9073.jpg","collect_num":"1426","food_str":"鲜鱼 姜 葱 蒜 花椒","num":1426},{"id":"10097","title":"家常煎豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10097.jpg","collect_num":"1419","food_str":"豆腐 新鲜红椒 青椒 葱花 油","num":1419},{"id":"10509","title":"水煮肉片","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10509.jpg","collect_num":"1342","food_str":"瘦猪肉 生菜 豆瓣酱 干辣椒 花椒","num":1342},{"id":"46968","title":"红糖苹果银耳汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/47/46968.jpg","collect_num":"1253","food_str":"银耳 苹果 红糖","num":1253},{"id":"10191","title":"麻婆豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10191.jpg","collect_num":"1224","food_str":"豆腐 肉末 生抽 白糖 芝麻油","num":1224},{"id":"2372","title":"皮蛋瘦肉粥","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2372.jpg","collect_num":"1151","food_str":"大米 皮蛋 猪肉 油条 香葱","num":1151},{"id":"2166","title":"蚂蚁上树","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2166.jpg","collect_num":"1145","food_str":"红薯粉 肉 姜 蒜 花椒","num":1145},{"id":"2262","title":"糖醋肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2262.jpg","collect_num":"1081","food_str":"猪肉 红椒 黄椒 洋葱 蛋清","num":1081},{"id":"9971","title":"鱼香豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9971.jpg","collect_num":"1010","food_str":"豆腐 木耳 胡萝卜 香葱 番茄酱","num":1010},{"id":"10172","title":"干煸四季豆","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10172.jpg","collect_num":"993","food_str":"四季豆 干辣椒 蒜头 酱油 糖","num":993},{"id":"2685","title":"胡萝卜肉末蒸蛋","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2685.jpg","collect_num":"929","food_str":"胡萝卜 肉 蛋 生抽 盐","num":929},{"id":"9972","title":"虎皮青椒","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9972.jpg","collect_num":"892","food_str":"青辣椒 大蒜 香醋 白糖 生抽","num":892},{"id":"10437","title":"叉烧排骨","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10437.jpg","collect_num":"806","food_str":"排骨 李锦记叉烧酱 植物油 清水 油菜","num":806},{"id":"2892","title":"\u201c五行\u201d彩蔬汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2892.jpg","collect_num":"761","food_str":"黑木耳 玉米 牛蒡 胡萝卜 西兰花","num":761},{"id":"2348","title":"麻辣肉丝面","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2348.jpg","collect_num":"760","food_str":"面条 肉丝 淀粉 酱油 辣椒","num":760},{"id":"10044","title":"土豆炖翅根","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10044.jpg","collect_num":"758","food_str":"土豆 翅根 葱 姜 料酒","num":758},{"id":"33783","title":"美人豆浆","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/34/33783.jpg","collect_num":"758","food_str":"黄豆 红豆 绿豆 黑豆 黑米","num":758}]
     */

    private int ret;
    private List<DataBean> data;

    public int getRet() {
        return ret;
    }

    public void setRet(int ret) {
        this.ret = ret;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * id : 8289
         * title : 油焖大虾
         * pic : http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg
         * collect_num : 1670
         * food_str : 大虾 葱 生姜 植物油 料酒
         * num : 1670
         */

        private String id;
        private String title;
        private String pic;
        private String collect_num;
        private String food_str;
        private int num;
        // todo 是否选中
        private boolean isSelect;

        public boolean isSelect() {
            return isSelect;
        }

        public void setSelect(boolean select) {
            isSelect = select;
        }

        public String getId() {
            return id;
        }

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

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getPic() {
            return pic;
        }

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

        public String getCollect_num() {
            return collect_num;
        }

        public void setCollect_num(String collect_num) {
            this.collect_num = collect_num;
        }

        public String getFood_str() {
            return food_str;
        }

        public void setFood_str(String food_str) {
            this.food_str = food_str;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }
    }
}

3.主UI布局

<?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=".ShoppingActivity">
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#DBC4C4">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="购物车"
                android:textColor="#fff"
                android:layout_centerInParent="true"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:text="编辑"
                android:gravity="center"
                android:id="@+id/tv_edit"/>
        </RelativeLayout>
    </androidx.appcompat.widget.Toolbar>
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:id="@+id/rv"></androidx.recyclerview.widget.RecyclerView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/cb_all"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_price"
            android:text="合计  0"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="结算"
            android:id="@+id/bt"/>
    </LinearLayout>
</LinearLayout>

4.UI代码片


/**
 * 1.实体类添加属性:isSelect 是否选中
 * 2.适配器根据isSelect判断是否选中+复选框点击
 * 3.全选和取消全选:修改集合中所有的数据源的isSelect--->刷新适配器----》计算总价
 * 4.用户反选:修改当前item的数据源isSelect--->刷新适配器----》计算总价
 */
public class ShoppingActivity extends AppCompatActivity {
    //编辑/删除 按钮
    private TextView tvEdit;
    private RecyclerView rv;
    //全选框
    private CheckBox cbAll;
    //价格
    private TextView tvPrice;
    //结算
    private Button bt;
    private XiaAdapter xiaAdapter;
    private List<XiaBean.DataBean> listBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping);
        initView();
        initData();
    }

    private void initData() {
        OkGo.<String>get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String json = response.body();
                        XiaBean xiaBean = new Gson().fromJson(json, XiaBean.class);
                        List<XiaBean.DataBean> list = xiaBean.getData();
                        listBeans.addAll(list);
                        xiaAdapter.notifyDataSetChanged();
                    }
                });
    }

    private void initView() {
        tvEdit = (TextView) findViewById(R.id.tv_edit);
        rv = (RecyclerView) findViewById(R.id.rv);
        cbAll = (CheckBox) findViewById(R.id.cb_all);
        tvPrice = (TextView) findViewById(R.id.tv_price);
        bt = (Button) findViewById(R.id.bt);
        xiaAdapter = new XiaAdapter(R.layout.item, listBeans);
        rv.setAdapter(xiaAdapter);
        rv.setLayoutManager(new LinearLayoutManager(this));

        //TODO 1:全选和取消全选:修改数据源 刷新适配器
        cbAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                for (XiaBean.DataBean listBean : listBeans) {
                    listBean.setSelect(isChecked);
                }
                xiaAdapter.notifyDataSetChanged();
                count();
            }
        });
        // TODO  子控件的点击
        xiaAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CheckBox checkBox = (CheckBox) view;
                listBeans.get(position).setSelect(checkBox.isChecked());//修改数据源
                xiaAdapter.notifyDataSetChanged();
                count();
            }
        });
        tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bt.setText("删除");
            }
        });
    }

    //TODO 2.计算价格
    private void count() {
        int count = 0;
        for (XiaBean.DataBean listBean : listBeans) {
            if (listBean.isSelect()) {//如果选中
                count += listBean.getNum();//累加
            }
        }
        tvPrice.setText("合计:" + count + "");
    }
}

5.item 小布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/cb"/>
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher_round"
        android:id="@+id/iv"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="11"
        android:id="@+id/tv"/>
</LinearLayout>

6.适配器

public class XiaAdapter extends BaseQuickAdapter<XiaBean.DataBean, BaseViewHolder> {
    public XiaAdapter(int layoutResId, @Nullable List<XiaBean.DataBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, XiaBean.DataBean item) {
         //价格
        helper.setText(R.id.tv,item.getNum()+"");
   //复选框是否选中
        helper.setChecked(R.id.cb,item.isSelect());
        Glide.with(mContext).load(item.getPic()).into((ImageView) helper.getView(R.id.iv));
  //TODO   点击事件
        helper.addOnClickListener(R.id.cb);
        CheckBox cb = helper.getView(R.id.cb);
        cb.setChecked(item.isSelect());
   }
}

7.效果图

在这里插入图片描述
在这里插入图片描述

over 。。。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值