购物车

布局

主布局
<?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">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rlv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        ></android.support.v7.widget.RecyclerView>


     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         >
         <CheckBox
             android:id="@+id/qx"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" />
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="全选"
             />

         <TextView
             android:id="@+id/zongjia"
             android:layout_marginLeft="40dp"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="总价"
             />


         <Button
             android:id="@+id/bt"
             android:layout_marginLeft="150dp"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="待支付"
             />

     </LinearLayout>

</LinearLayout>


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

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

        <CheckBox
            android:id="@+id/p_cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/p_text"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="呵呵呵"
            />

    </LinearLayout>





    <android.support.v7.widget.RecyclerView
        android:id="@+id/p_rlv"
        android:layout_width="match_parent"
        android:layout_height="200dp"></android.support.v7.widget.RecyclerView>


</LinearLayout>

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

    <CheckBox
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:gravity="center"
        />

    <ImageView
        android:id="@+id/img"
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/name"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:layout_weight="1"
            android:text="搜索"
            />

        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="123"
            />

    </LinearLayout>



    <crq.bwei.com.gouwuce.custom.Custom
        android:id="@+id/custom"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:gravity="center"
        android:layout_weight="1"/>










</LinearLayout>



Mainactiviy代码

 public class MainActivity extends AppCompatActivity  {

    private RecyclerView rlv;
    private CheckBox qx;
    private TextView zongjia;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //控件
        rlv = findViewById(R.id.rlv);
        qx = findViewById(R.id.qx);
        zongjia = findViewById(R.id.zongjia);
        rlv.setLayoutManager(new LinearLayoutManager(this));

        //数据
        String url="http://172.17.8.100/ks/product/getCarts?uid=51";
        OkHttpUtls.getInStence().getData(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Message message=new Message();
                message.what=0;
                message.obj=string;
                handler.sendMessage(message);
            }
        });

   }

   @SuppressLint("HandlerLeak")
   Handler handler=new Handler(){

       private MyPrentAdapter myPrentAdapter;

       @Override
       public void handleMessage(Message msg) {
           super.handleMessage(msg);
           switch (msg.what){
               case 0:
                   String obj= (String) msg.obj;
                   Gson gson = new Gson();
                   Shopping shopping = gson.fromJson(obj, Shopping.class);
                   List<Shopping.DataBean> data = shopping.getData();
                   myPrentAdapter = new MyPrentAdapter(MainActivity.this, data);
                   rlv.setAdapter(myPrentAdapter);

                   //全选
                   qx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                       @Override
                       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                           myPrentAdapter.checkAll(isChecked);//全部选中
                       }
                   });
                   //设置适配器回调总金额
                  myPrentAdapter.setParantTotalPriceLister(new MyPrentAdapter.ParantTotalPriceLister() {
                      @Override
                      public void p_totalprice(double totalprice) {
                          zongjia.setText("总价为:"+totalprice);
                      }
                  });


                   break;
           }

       }
   };
}

自定义加减器

public class Custom extends LinearLayout  implements View.OnClickListener {

    //数量
    int nums=0;
    private Button jia;
    private EditText et;
    private Button jian;

    public Custom(Context context) {
        super(context);
    }

    public Custom(Context context, AttributeSet attrs) {
        super(context, attrs);
        //找控件
        LayoutInflater.from(context).inflate(R.layout.custom,this,true);
        jia = findViewById(R.id.jia);
        et = findViewById(R.id.et);
        jian = findViewById(R.id.jian);
        //注册
        jia.setOnClickListener(this);
        et.setOnClickListener(this);
        jian.setOnClickListener(this);
        et.setText(nums+"");

    }

    public Custom(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.jia:


                //获得数据框的值
                String trim = et.getText().toString().trim();
                Integer integer = Integer.valueOf(trim);
                nums=integer+1;
                //加1设置数据框的值
                et.setText(nums+"");
                 //得到数量给接口回调
                if(onGetNumsListenr!=null){
                    onGetNumsListenr.getNums(nums);
                }
                break;


            case R.id.jian:

                 //获得数据框的值
                String trim1 = et.getText().toString().trim();
                Integer integer1 = Integer.valueOf(trim1);

                if(nums>1){
                    nums=integer1-1;
                    et.setText(nums+"");
                    //得到数量给接口回调
                    if(onGetNumsListenr!=null){
                        onGetNumsListenr.getNums(nums);
                    }

                    return;
                }else{
                    Toast.makeText(getContext(), "不能再减了", Toast.LENGTH_SHORT).show();


                }



                break;

        }
    }

    //设置数量
    public void setCount(int count) {
        et.setText(count+"");
    }

    //定义接口回调
    public interface  OnGetNumsListenr{
        void getNums(int num);
    }

    private OnGetNumsListenr onGetNumsListenr;

    public void setOnGetNumsListenr(OnGetNumsListenr onGetNumsListenr) {
        this.onGetNumsListenr = onGetNumsListenr;
    }
}
//布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/jia"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:text="+"
        />

    <EditText
        android:id="@+id/et"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="03"
        android:gravity="center"
         />


    <Button
        android:id="@+id/jian"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:text="-"
        />

</LinearLayout>

适配器

//父适配器
public class MyPrentAdapter extends RecyclerView.Adapter<MyPrentAdapter.MyViewhoder> {

    private Context context;
    private List<Shopping.DataBean> data;
    private MyChildAdapter myChildAdapter;

    public MyPrentAdapter(Context context, List<Shopping.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public MyViewhoder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context, R.layout.parent, null);
        MyViewhoder myViewhoder = new MyViewhoder(view);
        return myViewhoder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewhoder myViewhoder, int i) {
        myViewhoder.p_text.setText(data.get(i).getSellerName());//设置商铺
        myViewhoder.p_cb.setChecked(data.get(i).isCheck());//设置全选控制父类选中的状态
        //第二个适配器
        myViewhoder.p_rlv.setLayoutManager(new LinearLayoutManager(context));
        //数据
        myChildAdapter = new MyChildAdapter(context, data, i);
        myViewhoder.p_rlv.setAdapter(myChildAdapter);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    class MyViewhoder extends RecyclerView.ViewHolder {

        private final CheckBox p_cb;
        private final TextView p_text;
        private final RecyclerView p_rlv;

        public MyViewhoder(@NonNull View itemView) {
            super(itemView);
            p_cb = itemView.findViewById(R.id.p_cb);
            p_text = itemView.findViewById(R.id.p_text);
            p_rlv = itemView.findViewById(R.id.p_rlv);
        }
    }

    //全选的状态
    public void checkAll(boolean isChecked) {
        // 点击全选 则调用次方法

            for (int i = 0; i < data.size(); i++) {

                Shopping.DataBean shop = data.get(i);
                shop.setCheck(isChecked);// 设置父复选框的状态
               /* for (int j = 0; j < shop.getList().size(); j++) {
                    Shopping.DataBean.ListBean listBean = shop.getList().get(j);
                    listBean.setSelected(isChecked?1:0);//设置子属性选中状态
                }*/
            }
       /*     myChildAdapter.setTotalPriceLister(new MyChildAdapter.TotalPriceLister() {
                @Override
                public void totalprice(double totalprice) {
                    parantTotalPriceLister.p_totalprice(totalprice);
                }
            });*/
            notifyDataSetChanged();// 刷新数据

    }

    //接口回调存价格

    // 总价的自定义接口
    public interface ParantTotalPriceLister{
        void p_totalprice(double totalprice);
    }
    private ParantTotalPriceLister parantTotalPriceLister;
    //实例化自定义接口


    public void setParantTotalPriceLister(ParantTotalPriceLister parantTotalPriceLister) {
        this.parantTotalPriceLister = parantTotalPriceLister;
    }
}


//子适配器
public class MyChildAdapter  extends RecyclerView.Adapter<MyChildAdapter.MyViewHoder>{
    private Context context;
    private List<Shopping.DataBean> data;
    int j=0;

    public MyChildAdapter() {
    }

    public MyChildAdapter(Context context, List<Shopping.DataBean> data, int j) {
        this.context = context;
        this.data = data;
        this.j = j;
    }

    @NonNull
    @Override
    public MyViewHoder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context, R.layout.child, null);
        MyViewHoder myViewHoder = new MyViewHoder(view);
        return myViewHoder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHoder myViewHoder, final int i) {
        String images = data.get(j).getList().get(i).getImages();
        String[] split = images.split("\\|");

        if (split != null && split.length > 0) {

            Glide.with(context).load(split[0]).into(myViewHoder.img);
        }

      //  Glide.with(context).load(s).into(myViewHoder.img);

        myViewHoder.name.setText(data.get(j).getList().get(i).getSubhead());//标题
        myViewHoder.price.setText(data.get(j).getList().get(i).getPrice()+"");//价格
        myViewHoder.cb.setChecked(data.get(j).isCheck());//设置子复选框为父复选框的状态
        //子复选框的状态
       /* if (data.get(j).getList().get(i).getSelected()==1){
            myViewHoder.cb.setChecked(true);
        }else{
            myViewHoder.cb.setChecked(false);
        }
        // 点击选中,计算价格
        myViewHoder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 防治点击事件与选中发生冲突
                CheckBox checkBox = (CheckBox) v;
                data.get(j).getList().get(i).setSelected(checkBox.isChecked()?1:0);//设置此商品是否选中
                // 计算价格
                calculatePrice();
            }
        });
*/
        // 获得集合数量设置输入框数量
        myViewHoder.custom.setCount(data.get(j).getList().get(i).getNum());
        //监听回调数量设置集合数量
        myViewHoder.custom.setOnGetNumsListenr(new Custom.OnGetNumsListenr() {
            @Override
            public void getNums(int num) {
                data.get(j).getList().get(i).setNum(num);//设置数量
                calculatePrice();// 计算价格
            }
        });


    }

    @Override
    public int getItemCount() {
        return data.get(j).getList().size();
    }


    class MyViewHoder extends RecyclerView.ViewHolder {


        private final CheckBox cb;
        private final ImageView img;
        private final TextView name;
        private final TextView price;
        private final crq.bwei.com.gouwuce.custom.Custom custom;

        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            cb = itemView.findViewById(R.id.cb);
            img = itemView.findViewById(R.id.img);
            name = itemView.findViewById(R.id.name);
            price = itemView.findViewById(R.id.price);
            custom = itemView.findViewById(R.id.custom);
        }
    }


    /**
     * 计算总价格
     */
    public void calculatePrice() {
        double totalprice=0;
        for (int i = 0; i < data.size(); i++) {
            Shopping.DataBean dataBean = data.get(i);
            for (int j = 0; j < dataBean.getList().size(); j++) {
                Shopping.DataBean.ListBean goods = dataBean.getList().get(j);
                if (goods.getSelected() == 1) {
                    // 是选中状态的话则计算进去
                    totalprice = totalprice +goods.getNum()*goods.getPrice();
                }
            }
        }
        // 判断
        if (priceLister != null) {
            priceLister.totalprice(totalprice);// 将值利用接口回调传回Activity进行展示
        }
    }


    // 总价的自定义接口
    public interface TotalPriceLister{
        void totalprice(double totalprice);
   }
    private TotalPriceLister priceLister;
    //实例化自定义接口
    public void setTotalPriceLister(TotalPriceLister totalPriceLister) {
        this.priceLister = totalPriceLister;
    }

}





©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值