概述
本文实例为大家分享了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实现二级购物车的全选加反选、总价功能的全部内容,希望文章能够帮你解决Android实现二级购物车的全选加反选、总价功能所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复