当前位置:主页 > 查看内容

模拟购物车页面全选单选的金额累加

发布时间:2021-06-07 00:00| 位朋友查看

简介:1.前面部分的内容就不过多说明了就是OkGo请求数据然后封装适配器加入数据源先上代码 先上适配器的代码,注意这里使用的万能适配器加入依赖 implementation com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.0 package com . example . review ; import……

1.前面部分的内容就不过多说明了,就是OkGo请求数据然后封装适配器加入数据源,先上代码

先上适配器的代码,注意这里使用的万能适配器,加入依赖

implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.0'
package com.example.review;

import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class MyFoodAdaptr extends BaseQuickAdapter<FoodBean.DataBean, BaseViewHolder> {

    public MyFoodAdaptr(@Nullable List<FoodBean.DataBean> data) {
        super(R.layout.item_food, data);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, FoodBean.DataBean dataBean) {
        baseViewHolder.setText(R.id.item_text,dataBean.getTitle());
        Glide.with(getContext()).load(dataBean.getPic()).into((ImageView) baseViewHolder.getView(R.id.item_img));
        CheckBox box = baseViewHolder.getView(R.id.is_checked);

        addChildClickViewIds(R.id.is_checked);

        box.setChecked(dataBean.isDan_xuan());

        if (dataBean.isTan_chu()){
            box.setVisibility(View.VISIBLE);
        }else {
            box.setVisibility(View.GONE);
        }

    }
}

然后是子布局的页面详情

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


    <CheckBox
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/is_checked"></CheckBox>

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/item_img"></ImageView>

    <TextView
        android:gravity="center"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/item_text"></TextView>

</LinearLayout>

接下来就是实体类以及OkGo框架的内容

FoodBean实体类,可以Gson解析,注意加入Gson依赖

package com.example.review;

import java.util.List;

public class FoodBean {

    /**
     * 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":"1082","food_str":"猪肉 红椒 黄椒 洋葱 蛋清","num":1082},{"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;
    /**
     * id : 8289
     * title : 油焖大虾
     * pic : http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg
     * collect_num : 1670
     * food_str : 大虾 葱 生姜 植物油 料酒
     * num : 1670
     */

    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 {
        private String id;
        private String title;
        private String pic;
        private String collect_num;
        private String food_str;
        private int num;

        //这三个控制着长按弹出以及全选还有单选
        private boolean tan_chu = false;
        private boolean quan_xuan = false;
        private boolean dan_xuan = false;

        public boolean isDan_xuan() {
            return dan_xuan;
        }

        public void setDan_xuan(boolean dan_xuan) {
            this.dan_xuan = dan_xuan;
        }

        public boolean isTan_chu() {
            return tan_chu;
        }

        public void setTan_chu(boolean tan_chu) {
            this.tan_chu = tan_chu;
        }

        public boolean isQuan_xuan() {
            return quan_xuan;
        }

        public void setQuan_xuan(boolean quan_xuan) {
            this.quan_xuan = quan_xuan;
        }

        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;
        }
    }
}

然后是咱们的请求数据并且解析gson,实例化适配器加入数据源,在这里要注意初始化recyclerview的时候给他设置排列方式

	rv = (RecyclerView) findViewById(R.id.rv);
    rv.setLayoutManager(new LinearLayoutManager(this));
 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();
                FoodBean foodBean = new Gson().fromJson(json, FoodBean.class);
                List<FoodBean.DataBean> data = foodBean.getData();
                list.addAll(data);
                adaptr = new MyFoodAdaptr(list);
                rv.setAdapter(adaptr);
  }

重点来了

我们需要累加结算时候的价格,就需要子控件的点击事件,刚好这个版本的万能适配器可以加入子控件的点击事件,我们在adapter里面加入这个方法

	CheckBox box = baseViewHolder.getView(R.id.is_checked);
    addChildClickViewIds(R.id.is_checked);

选中之后金钱的计算方法

private void count() {
        num = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isDan_xuan()) {
                num += 1000;
            }
        }
        money.setText(num + "");
    }

接下来就是子控件的单点事件,具体步骤在注释里面了

adaptr.setOnItemChildClickListener(new OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                        //判断点击的控件ID是否和适配器里加入的子控件ID一样
                        if (view.getId()==R.id.is_checked){
                            //一样的话就强转成CheckBox类型
                            CheckBox box = (CheckBox) view;
                            //如果子控件box为选中状态那么当前点击的这个item里面的 Dan_Xuan 布尔值变为和 子控件box的值一样
                            if (box.isChecked()){
                                list.get(position).setDan_xuan(box.isChecked());
                                //coynt()就是金钱的计算方法
                                count();
                            }else {
                                list.get(position).setDan_xuan(box.isChecked());
                                count();
                            }
                            //刷新适配器
                            adapter.notifyDataSetChanged();

                        }
                    }
                });

现在展示一下原效果
在这里插入图片描述
然后再展示一下点击之后的效果
在这里插入图片描述
4.接下来就是全选了,大体的思想和上面的不变,给全选的id加一个监听事件,使子控件里面的值跟随全选按钮的值变动而变动,话不多说,上代码

allChecked.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).setDan_xuan(isChecked);
                        }
                        count();
                        adaptr.notifyDataSetChanged();
                    }
                });

展示一下效果
在这里插入图片描述
Finally

这就是模拟购物车页面全选单选的金额累加,写的有点乱,下次改进!!!!!!!!!

;原文链接:https://blog.csdn.net/AM12idi/article/details/115565442
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐