创建bean类
bean类中定义boolean类型的变量,生成getter、setter方法
public class ShopCartBean implements Serializable {
private String msg;
private String code;
private List data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
public static class DataBean {
private String sellerName;
private String sellerid;
private List list;
private boolean isCheck = false;
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean check) {
isCheck = check;
}
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public String getSellerid() {
return sellerid;
}
public void setSellerid(String sellerid) {
this.sellerid = sellerid;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public static class ListBean {
private String bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int num;
private String pid;
private int price;
private String pscid;
private String selected;
private String sellerid;
private String subhead;
private String title;
private boolean isCheck = false;
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean check) {
isCheck = check;
}
public String getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(String bargainPrice) {
this.bargainPrice = bargainPrice;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDetailUrl() {
return detailUrl;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getPscid() {
return pscid;
}
public void setPscid(String pscid) {
this.pscid = pscid;
}
public String getSelected() {
return selected;
}
public void setSelected(String selected) {
this.selected = selected;
}
public String getSellerid() {
return sellerid;
}
public void setSellerid(String sellerid) {
this.sellerid = sellerid;
}
public String getSubhead() {
return subhead;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
}
Activity或Fragment(这里用的是Fragment)
public class ShopCarFragment extends Fragment implements IShopCartView {
@BindView(R.id.shopcart_recycler_view)
RecyclerView shopcartRecyclerView;
Unbinder unbinder;
@BindView(R.id.shop_checkbox)
CheckBox shopCheckbox;
@BindView(R.id.shop_total)
TextView shopTotal;
@BindView(R.id.shop_jie)
Button shopJie;
private Map map;
private ShopCartPresenter shopCartPresenter;
private List cartBeanData;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shop_car_layout, container, false);
map = new HashMap<>();
unbinder = ButterKnife.bind(this, view);
shopCartPresenter = new ShopCartPresenter(ShopCarFragment.this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
shopcartRecyclerView.setLayoutManager(linearLayoutManager);
//shopcartRecyclerView.addItemDecoration(new SpaceItemDecoration(20));
map.put("uid", "71");
shopCartPresenter.getPresenterData(map);
return view;
}
@Override
public void getViewData(String data) {
Gson gson = new Gson();
ShopCartBean shopCartBean = gson.fromJson(data, ShopCartBean.class);
cartBeanData = shopCartBean.getData();
//创建适配器
final MyShopCartAdapter shopCartAdapter = new MyShopCartAdapter(getActivity(), cartBeanData);
shopcartRecyclerView.setAdapter(shopCartAdapter);
shopCartAdapter.setShopCallBackListener(new MyShopCartAdapter.ShopCallBackListener() {
@Override
public void callBack(List list) {
//在这里重新遍历已经改状态后的数据,
// 这里不能break跳出,因为还需要计算后面点击商品的价格和数目,所以必须跑完整个循环
double totalPrice = 0;
//勾选商品的数量,不是该商品购买的数量
int num = 0;
//所有商品总数,和上面的数量做比对,如果两者相等,则说明全选
int totalNum = 0;
for (int i = 0; i < list.size(); i++) {
List listAll = list.get(i).getList();
for (int j = 0; j < listAll.size(); j++) {
totalNum = totalNum + listAll.get(j).getNum();
//取选中的状态
if (listAll.get(j).isCheck()) {
totalPrice += listAll.get(j).getPrice() * listAll.get(j).getNum();
num += listAll.get(j ).getNum();
}
}
}
if (num < totalNum) {
shopCheckbox.setChecked(false);
}else {
shopCheckbox.setChecked(true);
}
shopTotal.setText("合计:¥"+totalPrice);
shopJie.setText("去计算("+num+")");
}
});
shopCheckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkSeller(shopCheckbox.isChecked());
shopCartAdapter.notifyDataSetChanged();
}
});
if (cartBeanData!=null){
cartBeanData.remove(0);
shopCartAdapter.setList(cartBeanData);
shopCartAdapter.notifyDataSetChanged();
}
}
private void checkSeller(boolean bool) {
int totalPrice = 0;
int num = 0;
for (int a = 0; a < cartBeanData.size(); a++) {
//遍历商家,改变状态
ShopCartBean.DataBean dataBean = cartBeanData.get(a);
dataBean.setCheck(bool);
List listAll = cartBeanData.get(a).getList();
for (int i = 0; i < listAll.size(); i++) {
//遍历商品,改变状态
listAll.get(i).setCheck(bool);
totalPrice += listAll.get(i).getPrice() * listAll.get(i).getNum();
num += listAll.get(i).getNum();
}
}
if (bool) {
shopTotal.setText("合计:" + totalPrice);
shopJie.setText("去结算(" + num + ")");
} else {
shopTotal.setText("合计:0.00");
shopJie.setText("去结算(0)");
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}
创建商家的Adapter
public class MyShopCartAdapter extends RecyclerView.Adapter {
private Context context;
private List cartBeanData;
private List listBeans;
private ShopCallBackListener shopCallBackListener;
public void setShopCallBackListener(ShopCallBackListener shopCallBackListener) {
this.shopCallBackListener = shopCallBackListener;
}
public MyShopCartAdapter(Context context, List cartBeanData) {
this.context = context;
this.cartBeanData = cartBeanData;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.merchant_layout, viewGroup, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
viewHolder.merchantCheck.setText(cartBeanData.get(i).getSellerName());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
viewHolder.merchantRecyclerView.setLayoutManager(linearLayoutManager);
listBeans = cartBeanData.get(i).getList();
//创建适配器
final MyShopCartItmeAdapter shopCartItmeAdapter = new MyShopCartItmeAdapter(context,listBeans);
viewHolder.merchantRecyclerView.setAdapter(shopCartItmeAdapter);
//viewHolder.merchantRecyclerView.addItemDecoration(new SpaceItemDecoration(10));
viewHolder.merchantCheck.setChecked(cartBeanData.get(i).isCheck());
shopCartItmeAdapter.setShopCallBackListener(new MyShopCartItmeAdapter.ShopCallBackListener() {
@Override
public void callBack() {
if (shopCartItmeAdapter!=null){
shopCallBackListener.callBack(cartBeanData);
}
List listBeanList = cartBeanData.get(i).getList();
boolean isAllChecked = true;
for (ShopCartBean.DataBean.ListBean bean: listBeanList) {
if (!bean.isCheck()){
isAllChecked= false;
break;
}
}
viewHolder.merchantCheck.setChecked(isAllChecked);
cartBeanData.get(i).setCheck(isAllChecked);
}
});
viewHolder.merchantCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cartBeanData.get(i).setCheck(viewHolder.merchantCheck.isChecked());
shopCartItmeAdapter.selectAllOrRemoveAll(viewHolder.merchantCheck.isChecked());
}
});
}
@Override
public int getItemCount() {
return cartBeanData.size();
}
@OnClick(R.id.merchant_check)
public void onViewClicked() {
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.merchant_check)
CheckBox merchantCheck;
@BindView(R.id.merchant_recycler_view)
RecyclerView merchantRecyclerView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
public interface ShopCallBackListener {
void callBack(List list);
}
public void setList(List cartBeanData){
this.cartBeanData = cartBeanData;
notifyDataSetChanged();
}
}
创建商品的adapter
public class MyShopCartItmeAdapter extends RecyclerView.Adapter {
private Context context;
private List listBeans;
private ShopCallBackListener shopCallBackListener;
public void setShopCallBackListener(ShopCallBackListener shopCallBackListener) {
this.shopCallBackListener = shopCallBackListener;
}
public MyShopCartItmeAdapter(Context context, List listBeans) {
this.context = context;
this.listBeans = listBeans;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.shopcart_itme_layout, viewGroup, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
Glide.with(context).load(listBeans.get(i).getImages().split("\\|")[0].replace("https", "http")).into(viewHolder.goodsImg);
viewHolder.googsTitle.setText(listBeans.get(i).getTitle());
viewHolder.goodsPrice.setText("¥:" + listBeans.get(i).getPrice() + "元");
viewHolder.custom_edit_text.setText(listBeans.get(i).getNum() + "");
viewHolder.customView.setClickListener(new CustomView.OnAddOrDelClickListener() {
@Override
public void onAddClick(View view) {
int number = viewHolder.customView.getNumber();
number++;
viewHolder.customView.setNumber(number);
listBeans.get(i).setNum(number);
}
@Override
public void onDelClick(View view) {
int num = viewHolder.customView.getNumber();
num--;
viewHolder.customView.setNumber(num);
listBeans.get(i).setNum(num);
}
});
//根据记录状态改变勾选
viewHolder.goodsCheckbox.setChecked(listBeans.get(i).isCheck());
//选中改变的监听
viewHolder.goodsCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//优先改变自己的状态
listBeans.get(i).setCheck(isChecked);
if (shopCallBackListener != null) {
shopCallBackListener.callBack();
}
}
});
viewHolder.customView.setData(this, listBeans, i);
viewHolder.customView.setCallBackListener(new CustomView.CallBackListener() {
@Override
public void mycallBack() {
if (shopCallBackListener != null) {
shopCallBackListener.callBack();
}
}
});
}
@Override
public int getItemCount() {
return listBeans.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final TextView custom_edit_text;
@BindView(R.id.goods_checkbox)
CheckBox goodsCheckbox;
@BindView(R.id.goods_img)
ImageView goodsImg;
@BindView(R.id.googs_title)
TextView googsTitle;
@BindView(R.id.goods_price)
TextView goodsPrice;
@BindView(R.id.custom_view)
CustomView customView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
custom_edit_text = itemView.findViewById(R.id.custom_edit_text);
}
}
//在子商品中的adapter中,修改子商品的全选和反选
public void selectAllOrRemoveAll(boolean selectAll) {
for (ShopCartBean.DataBean.ListBean listBean : listBeans) {
listBean.setCheck(selectAll);
}
notifyDataSetChanged();
}
//定义接口
public interface ShopCallBackListener {
void callBack();
}
}
自定义view类(加加、减减功能)
public class CustomView extends RelativeLayout {
@BindView(R.id.custom_del)
ImageView customDel;
@BindView(R.id.custom_edit_text)
EditText customEditText;
@BindView(R.id.custom_add)
ImageView customAdd;
private OnAddOrDelClickListener clickListener;
private MyShopCartItmeAdapter myShopCartItemAdapter;
private CallBackListener call1BackListener;
private List list = new ArrayList<>();
private int position;
int num = 0;
public void setCallBackListener(CallBackListener callBackListener) {
this.call1BackListener = callBackListener;
}
public void setClickListener(OnAddOrDelClickListener clickListener) {
this.clickListener = clickListener;
}
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
View view = View.inflate(context, R.layout.custom_layout, null);
ButterKnife.bind(this, view);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.edit_shopcart);
String num = typedArray.getString(R.styleable.edit_shopcart_num);
typedArray.recycle();
customEditText.setText(num);
addView(view);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void setData(MyShopCartItmeAdapter myShopCartItemAdapter, List list, int i) {
this.myShopCartItemAdapter = myShopCartItemAdapter;
this.list = list;
position = i;
num = list.get(i).getNum();
customEditText.setText(num + "");
}
@OnClick({R.id.custom_del, R.id.custom_add})
public void onViewClicked(View view) {
String string = customEditText.getText().toString();
switch (view.getId()) {
case R.id.custom_del:
if (string.length() > 0) {
clickListener.onDelClick(view);
call1BackListener.mycallBack();
myShopCartItemAdapter.notifyItemChanged(position);
} else {
mToast();
}
break;
case R.id.custom_add:
if (string.length() > 0) {
clickListener.onAddClick(view);
call1BackListener.mycallBack();
myShopCartItemAdapter.notifyItemChanged(position);
} else {
mToast();
}
break;
}
}
public void setNumber(int num) {
if (num > 0) {
customEditText.setText(num + "");
} else {
toast();
}
}
public int getNumber() {
String numStr = customEditText.getText().toString().trim();
num = Integer.valueOf(numStr);
return num;
}
private void toast() {
Toast.makeText(getContext(), "不能为0", Toast.LENGTH_SHORT).show();
}
private void mToast() {
Toast.makeText(getContext(), "不能为空", Toast.LENGTH_SHORT).show();
}
public interface OnAddOrDelClickListener {
void onAddClick(View view);
void onDelClick(View view);
}
public interface CallBackListener {
void mycallBack();
}
}
自定义view布局