购物车二级列表+全选全不选+联动价格

二级适配器
public class CartsAdapter extends RecyclerView.Adapter {
private Context context;
private List cartsdata;

public CartsAdapter(Context context, List cartsdata) {
    this.context = context;
    this.cartsdata = cartsdata;
}

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

@Override
public void onBindViewHolder(@NonNull CartsAdapter.ViewHolder viewHolder, int i) {
        viewHolder.shouhuo2_img.setImageURI(cartsdata.get(i).getImages());
        viewHolder.shouhuo2_title.setText(cartsdata.get(i).getTitle());
        viewHolder.shouhuo2_price.setText(cartsdata.get(i).getBargainPrice()+"");

    viewHolder.checkbox_item1.setChecked(cartsdata.get(i).isProductCheckbox);
}

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

public class ViewHolder extends RecyclerView.ViewHolder {

    private final CheckBox checkbox_item1;
    private final TextView shouhuo2_title;
    private final TextView shouhuo2_price;
    private final SimpleDraweeView shouhuo2_img;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        checkbox_item1 = itemView.findViewById(R.id.checkbox_item1);
        shouhuo2_title = itemView.findViewById(R.id.shouhuo2_title);
        shouhuo2_price = itemView.findViewById(R.id.shouhuo2_price);
        shouhuo2_img = itemView.findViewById(R.id.shouhuo2_img);
    }
}

}

一级适配器
public class CartAdapter extends RecyclerView.Adapter {
private Context context;
private List cartdata;
private CartsAdapter cartsAdapter;

public CartAdapter(Context context, List cartdata) {
    this.context = context;
    this.cartdata = cartdata;

    Log.e("xqqq",cartdata+"");


}

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

@Override
public void onBindViewHolder(@NonNull final CartAdapter.ViewHolder viewHolder, final int i) {
    viewHolder.sj.setText(cartdata.get(i).getSellerName());


    viewHolder.rv.setLayoutManager(new LinearLayoutManager(context));
    cartsAdapter = new CartsAdapter(context, cartdata.get(i).getList());
    viewHolder.rv.setAdapter(cartsAdapter);
    viewHolder.checkbox_item.setChecked(cartdata.get(i).isCheckbox);

    //全选全不选
    viewHolder.checkbox_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            for (int j = 0; j < cartdata.get(i).getList().size(); j++) {
                cartdata.get(i).getList().get(j).setProductCheckbox(isChecked);
            }
            cartsAdapter.notifyDataSetChanged();
        }
    });

}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    private final TextView sj;
    private final CheckBox checkbox_item;
    private final RecyclerView rv;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        sj = itemView.findViewById(R.id.sj);
        checkbox_item = itemView.findViewById(R.id.checkbox_item);
        rv = itemView.findViewById(R.id.rv);
    }
}

}
主页面
public class FragmentCart extends Fragment implements DataView {
@BindView(R.id.cart_rv)
RecyclerView cartRv;
@BindView(R.id.checkbox)
CheckBox checkbox;
@BindView(R.id.js)
Button js;
Unbinder unbinder;
private ShowPresenter showPresenter;
private CartAdapter cartAdapter;
private CartBean cartBean;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragmentcart, container, false);
    //ButterKnife
    unbinder = ButterKnife.bind(this, view);
    initData();
    return view;
}

private void initData() {
    showPresenter = new ShowPresenter(this);
    HashMap map = new HashMap<>();
    map.put("uid","51");
    showPresenter.getData(Api.CART,map,2);
    showPresenter.attchView(this);
}

@Override
public void success(T result) {
    if (result instanceof CartBean){
        cartBean = (CartBean) result;
        cartRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        Log.e("xqqq", cartBean.getMsg()+"");
        cartAdapter = new CartAdapter(getActivity(), cartBean.getData());
        cartRv.setAdapter(cartAdapter);
        //全选全不选
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                for (int i = 0; i < cartBean.getData().size(); i++) {
                    cartBean.getData().get(i).setCheckbox(checkbox.isChecked());
                    for (int j = 0; j < cartBean.getData().get(i).getList().size(); j++) {
                        cartBean.getData().get(i).getList().get(j).setProductCheckbox(checkbox.isChecked());
                    }
                }
                cartAdapter.notifyDataSetChanged();
                jiaqian();
            }
        });
    }
}
//计算总价
private void jiaqian() {
    double totalprice=0;
    for (CartBean.DataBean dataBean : cartBean.getData()) {
        for (CartBean.DataBean.ListBean listBean : dataBean.getList()) {
            totalprice+=listBean.getPrice();
        }
    }
    checkbox.setText("¥:"+totalprice);
}

@Override
public void failure(T error) {

}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
    showPresenter.detachView();
}

}

一级布局



    
        
            
            
            
        

    


二级布局




    

        
            
            
            

                
                


            
        
        
            
        
    

你可能感兴趣的:(购物车二级列表+全选全不选+联动价格)