public class Frag01 extends Fragment implements BaseView {
private RecyclerView mRecycler;
private TextView mTvPrice;
private TextView mTvMoney;
private ShopCarAdapter adapter;
private BasePresenter presenter;
private LinearLayoutManager manager;
private CheckBox mCheckeBox;
private String url = “http://172.17.8.100/ks/product/getCarts?uid=51”;
private List
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag01, container, false);
mRecycler = view.findViewById(R.id.recycler);
mTvPrice = view.findViewById(R.id.tv_allprice);
mTvMoney = view.findViewById(R.id.tv_money);
mCheckeBox = view.findViewById(R.id.checkbox_all);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
presenter = new BasePresenter(new BaseModule(), this);
manager = new LinearLayoutManager(getActivity());
manager.setOrientation(LinearLayoutManager.VERTICAL);
mRecycler.setLayoutManager(manager);
adapter = new ShopCarAdapter(getActivity());
mRecycler.setAdapter(adapter);
mCheckeBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked = mCheckeBox.isChecked();
float allPrice = 0;
int allNum = 0;
for (int i = 0; i < datas.size(); i++) {
datas.get(i).setChecked(isChecked);
List list = datas.get(i).getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setChecked(isChecked);
if (isChecked) {//全选
float price = list.get(j).getPrice();
int num = list.get(j).getNum();
allPrice = allPrice + price * num;
allNum = allNum + num;
} else {//不全选
allNum = 0;
allPrice = 0;
}
}
}
mTvPrice.setText(allPrice + "");//设置总价
mTvMoney.setText("去结算" + allNum + "");//设置数量
adapter.setList(datas);
}
});
adapter.setOnCallBackListener(new ShopCarAdapter.OnCallBackListener() {
@Override
public void changeData(List list) {
float allPrice = 0;
int allNum = 0;
for (int i = 0; i < list.size(); i++) {
boolean ischecked = list.get(i).isChecked();
if (ischecked) {//商家选中
List listC = list.get(i).getList();
for (int j = 0; j < listC.size(); j++) {
float price = listC.get(j).getPrice();
int num = listC.get(j).getNum();
allPrice = allPrice + price * num;
allNum = allNum + num;
}
} else {//商家未选中,有可能商家里面的商品选中了
List listC = list.get(i).getList();
for (int j = 0; j < listC.size(); j++) {
if (listC.get(j).isChecked()) {
float price = listC.get(j).getPrice();
int num = listC.get(j).getNum();
allPrice = allPrice + price * num;
allNum = allNum + num;
}
}
}
}
mTvPrice.setText(allPrice + "");//设置总价
mTvMoney.setText("去结算(" + allNum + ")");//设置数量
}
});
presenter.doGet(0, url);
}
@Override
public void success(int type, String data) {
ShopCarBean bean = new Gson().fromJson(data, ShopCarBean.class);
datas = bean.getData();//所有的商家以及商家下的商品集合
datas.remove(0);
adapter.setList(datas);
}
@Override
public void fail(String error) {
}
}
ShopCarAdapter
public class ShopCarAdapter extends RecyclerView.Adapter
private Context context;
private List
public ShopCarAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public ShopCarAdapter.ShopCarViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.adapter_item, null);
ShopCarViewHolder shopCarViewHolder = new ShopCarViewHolder(view);
return shopCarViewHolder;
}
@Override
public void onBindViewHolder(@NonNull final ShopCarAdapter.ShopCarViewHolder shopCarViewHolder, final int i) {
shopCarViewHolder.mTitle.setText(list.get(i).getSellerName());//设置商家名字
shopCarViewHolder.mCheckBox.setChecked(list.get(i).isChecked());//改变商家是否选中
List listShop = list.get(i).getList();//获取商家下对应得商品的集合
ShopCarItemAdapter shopCarItemAdapter = new ShopCarItemAdapter(context, listShop);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
shopCarViewHolder.mRecycler.setLayoutManager(linearLayoutManager);//一定要记住设置布局管理器
shopCarViewHolder.mRecycler.setAdapter(shopCarItemAdapter);
shopCarItemAdapter.setOnCallBackListener(new ShopCarItemAdapter.OnCallBackListener() {
@Override
public void changeData(List listShop) {
int checkedNum=0;
for (int i = 0; i < listShop.size(); i++) {
boolean checked=listShop.get(i).isChecked();
if(checked){
checkedNum++;
}
}
if(checkedNum==listShop.size()){//商家下的商品 全选
shopCarViewHolder.mCheckBox.setChecked(true);
}else{//不是全选
shopCarViewHolder.mCheckBox.setChecked(false);
}
if(mOnCallBackListener!=null){
mOnCallBackListener.changeData(list);//回传给调用页面
}
}
});
//商家的checkbox
shopCarViewHolder.mCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked=shopCarViewHolder.mCheckBox.isChecked();
list.get(i).setChecked(isChecked);//赋值
List listShop = list.get(i).getList();//获取商家下对应得商品的集合
for (int i = 0; i < listShop.size(); i++) {
listShop.get(i).setChecked(isChecked);//设置商品是否选中
}
if(mOnCallBackListener!=null){
mOnCallBackListener.changeData(list);//回传给调用页面
}
//刷新适配器
notifyItemChanged(i);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
//传递数据
public void setList(List list) {
this.list = list;
notifyDataSetChanged();
}
public class ShopCarViewHolder extends RecyclerView.ViewHolder {
CheckBox mCheckBox;
RecyclerView mRecycler;
TextView mTitle;
public ShopCarViewHolder(@NonNull View itemView) {
super(itemView);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
mRecycler = (RecyclerView) itemView.findViewById(R.id.recycler);
mCheckBox = (CheckBox) itemView.findViewById(R.id.checkbox);
}
}
private OnCallBackListener mOnCallBackListener;
public void setOnCallBackListener(OnCallBackListener mOnCallBackListener){
this.mOnCallBackListener=mOnCallBackListener;
}
public interface OnCallBackListener{
void changeData(List list);
}
}
ShopCarItemAdapter
public class ShopCarItemAdapter extends RecyclerView.Adapter
private Context context;
private List
public ShopCarItemAdapter(Context context, List listShop) {
this.context = context;
this.listShop = listShop;
}
@NonNull
@Override
public ShopCarItemAdapter.ShopCarItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.adapter_item_child, null);
ShopCarItemViewHolder shopCarItemViewHolder = new ShopCarItemViewHolder(view);
return shopCarItemViewHolder;
}
@Override
public void onBindViewHolder(@NonNull final ShopCarItemViewHolder shopCarItemViewHolder, final int i) {
Glide.with(context).load(listShop.get(i).getImages()).into(shopCarItemViewHolder.mImage);
shopCarItemViewHolder.mTitle.setText(listShop.get(i).getTitle());
shopCarItemViewHolder.mPrice.setText(listShop.get(i).getPrice() + "");
shopCarItemViewHolder.mCheckBox.setChecked(listShop.get(i).isChecked());//设置商品是否选中
shopCarItemViewHolder.mShopCarAddView.setNum(listShop.get(i).getNum());//传递数量
//接收数量发生变化
shopCarItemViewHolder.mShopCarAddView.setOnNumCallBackListener(new ShopCarAddView.NumListener() {
@Override
public void num(int num) {
listShop.get(i).setNum(num);
if (listener!=null){
listener.changeData(listShop);
}
}
});
//商品的CheckBox
shopCarItemViewHolder.mCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked= shopCarItemViewHolder.mCheckBox.isChecked();
listShop.get(i).setChecked(isChecked);
if(listener!=null){
listener.changeData(listShop);
}
}
});
}
@Override
public int getItemCount() {
return listShop.size();
}
public class ShopCarItemViewHolder extends RecyclerView.ViewHolder {
ShopCarAddView mShopCarAddView;
CheckBox mCheckBox;
TextView mTitle, mPrice;
ImageView mImage;
public ShopCarItemViewHolder(@NonNull View itemView) {
super(itemView);
mImage = (ImageView) itemView.findViewById(R.id.iv_shop);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
mPrice = (TextView) itemView.findViewById(R.id.tv_price);
mCheckBox=(CheckBox)itemView.findViewById(R.id.checkbox);
mShopCarAddView=(ShopCarAddView)itemView.findViewById(R.id.shop_car_view);
}
}
private OnCallBackListener listener;
public void setOnCallBackListener(OnCallBackListener listener){
this.listener=listener;
}
public interface OnCallBackListener{
void changeData(List listShop);
}
}