时间:2021-05-19
本文实例为大家分享了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); } }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了Android实现二级列表购物车功能的具体代码,供大家参考,具体内容如下MainActivity:packagecom.baway.twos
购物车里的功能无非是商品数量的加减、商品删除、全选反选等操作,其实现过程如下所示:1.html代码:购物车空空如也,快去选购吧全选序号商品名称数量单价小计操作1
微信小程序,这里实现购物车功能的小demo,有需要此功能的朋友可以参考下。摘要:加减商品数量,汇总价格,全选与全不选设计思路:一、从网络上传入以下Json数据格
js,jq实现全选、反选功能:js:思路:1.点击全选按钮实现,下面内容全选,与反选2.下面有一个没有选中时,全选按钮不勾选,当全部选中时才全选。这里引入一个计
JS计算购物车中商品总价,供大家参考,具体内容如下题目要求:购物车中有若干商品信息,其中包括商品的名称、单价、数量,计算购物车中商品的总价。具体思路:商品信息通