Android实现二级购物车的全选加反选、总价功能

这篇文章主要为大家详细介绍了Android实现二级购物车的全选加反选、总价功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现二级购物车的全选加反选、总价的具体代码,供大家参考,具体内容如下

MainActivity


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.ArrayList;

import wangyaowei.bw.com.shopcart.adapter.ShopAdapter;
import wangyaowei.bw.com.shopcart.bean.DataBean;
import wangyaowei.bw.com.shopcart.bean.ListBean;
import wangyaowei.bw.com.shopcart.bean.ShopBean;
import wangyaowei.bw.com.shopcart.presenter.GoodsPresenter;
import wangyaowei.bw.com.shopcart.view.GoodsView;

public class MainActivity extends AppCompatActivity implements GoodsView {

 private RecyclerView rlv_shop;
 private GoodsPresenter goodsPresenter;
 private CheckBox box_all;
 private ShopAdapter shopAdapter;
 private ArrayList<DataBean> data;
 public TextView sum;
 public int nums;
 public Double prices;
 public double total;

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

 sum = findViewById(R.id.sum);
 rlv_shop = findViewById(R.id.rlv_shop);
 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
 rlv_shop.setLayoutManager(linearLayoutManager);

 goodsPresenter = new GoodsPresenter(this);
 goodsPresenter.relaed();
 box_all = findViewById(R.id.box_all);

 }

 @Override
 public void onResult(ArrayList<DataBean> data) {
 this.data=data;
 shopAdapter = new ShopAdapter(MainActivity.this, data);
 rlv_shop.setAdapter(shopAdapter);
 setaaa();

 }

 private void setaaa() {

 box_all.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  if(box_all.isChecked()){

   for(int i=0;i<data.size();i++){
   DataBean dataBean = data.get(i);
   dataBean.setFlag(true);
   ArrayList<ListBean> list = dataBean.getList();
   for (int j = 0; j < list.size(); j++) {
    ListBean listBean = list.get(j);
    listBean.setFlags(true);
   }
   }

  }
  else {
   for(int i=0;i<data.size();i++){
   DataBean dataBean = data.get(i);
   dataBean.setFlag(false);
   ArrayList<ListBean> list = dataBean.getList();
   for (int j = 0; j < list.size(); j++) {
    ListBean listBean = list.get(j);
    listBean.setFlags(false);
   }
   }
  }
  shopAdapter.notifyDataSetChanged();
  getPrice();
  }
 });
 }

 private void getPrice() {
 total = 0;
 int sums=0;
 for (int i = 0; i < data.size(); i++) {
  DataBean dataBean = data.get(i);
  ArrayList<ListBean> list = dataBean.getList();
  for (int j = 0; j < list.size(); j++) {
  ListBean listBean = list.get(j);
  if (listBean.isFlags()){
   String num = listBean.getNum();
   nums = Integer.valueOf(num);
   String price =listBean.getPrice();
   prices = Double.valueOf(price);
   total += nums * prices;
   sums++;
  }
  }

 }

 sum.setText(total +"");

 }
}

一级适配器


public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.MyViewHolder> {
 Context context;
 ArrayList<DataBean> beans;


 public ShopAdapter(Context context, ArrayList<DataBean> beans) {
 this.context = context;
 this.beans = beans;
 }

 @NonNull
 @Override
 public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
 View view=LayoutInflater.from(context).inflate(R.layout.shop_layout,null,false);
 MyViewHolder myViewHolder = new MyViewHolder(view);
 return myViewHolder;
 }

 @Override
 public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
 myViewHolder.name_shop.setText(beans.get(i).getSellerName());

 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
 myViewHolder.rlv_goods.setLayoutManager(linearLayoutManager);

 ArrayList<ListBean> list = beans.get(i).getList();
 GoodsAdapter goodsAdapter = new GoodsAdapter(context, list);

 myViewHolder.rlv_goods.setAdapter(goodsAdapter);
 myViewHolder.box_shop.setChecked(beans.get(i).isFlag());
 }

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

 public class MyViewHolder extends RecyclerView.ViewHolder {

 private final TextView name_shop;
 private final CheckBox box_shop;
 private final RecyclerView rlv_goods;
 public MyViewHolder(@NonNull View itemView) {
  super(itemView);
  name_shop = itemView.findViewById(R.id.name_shop);
  rlv_goods = itemView.findViewById(R.id.rlv_goods);
  box_shop = itemView.findViewById(R.id.box_shop);
 }
 }
}

二级适配器


public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.MyViewHolder> {
 Context context;
 ArrayList<ListBean> beans;
 private int shu;

 public GoodsAdapter(Context context, ArrayList<ListBean> beans) {
 this.context = context;
 this.beans = beans;
 }

 @NonNull
 @Override
 public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
 View view=LayoutInflater.from(context).inflate(R.layout.goods_layout,null,false);
 MyViewHolder myViewHolder = new MyViewHolder(view);
 return myViewHolder;
 }

 @Override
 public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, int i) {
 myViewHolder.name_goods.setText(beans.get(i).getTitle());
 myViewHolder.price_goods.setText(beans.get(i).getPrice()+"");

 String num = beans.get(i).getNum();
 shu = Integer.parseInt(num);
 myViewHolder.addview.nums.setText(shu +"");

 Glide.with(context).load(beans.get(i).getImages()).into(myViewHolder.img_goods);
 myViewHolder.box_goods.setChecked(beans.get(i).isFlags());

 myViewHolder.addview.jia.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  shu++;
  myViewHolder.addview.nums.setText(shu +"");
  }
 });
 myViewHolder.addview.jian.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  if (shu>=1){
   shu--;
   myViewHolder.addview.nums.setText(shu +"");
  }else {
   Toast.makeText(context, "已经最少不能减了", Toast.LENGTH_SHORT).show();
  }
  }
 });

 }

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

 public class MyViewHolder extends RecyclerView.ViewHolder {

 private final TextView name_goods,price_goods;
 private final ImageView img_goods;
 private final AddView addview;
 private final CheckBox box_goods;

 public MyViewHolder(@NonNull View itemView) {
  super(itemView);
  name_goods = itemView.findViewById(R.id.name_goods);
  price_goods = itemView.findViewById(R.id.price_goods);
  img_goods = itemView.findViewById(R.id.img_goods);
  addview = itemView.findViewById(R.id.addview);
  box_goods = itemView.findViewById(R.id.box_goods);
 }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

这篇文章主要介绍了android中关于call拨号功能实现的记录,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要介绍了Android布局生成分享图片,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要介绍了Android8.1原生系统网络感叹号消除的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
最近小编接到一个新的需求,需要将8.1 设备的来电功能和短信功能都屏蔽掉,特殊产品就是特殊定制。接下来通过本文给大家介绍Android8.1 通过黑名单屏蔽系统短信和来电功能,需要的朋友参考下吧
这篇文章主要给大家介绍了关于Android动画教程之属性动画的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
这篇文章主要给大家介绍了关于Android如何实现跨进程接口回掉的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧