购物子
package com.example.democart.adpter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.democart.R;
import com.example.democart.bean.CartBean;
import com.example.democart.callback.CartCallback;
import com.example.democart.wedigt.AddMinusView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.List;
public class ProductAdpter extends XRecyclerView.Adapter<ProductAdpter.MyVH> {
private Context context;
private List<CartBean.Cart.Product> list;
private CartCallback cartCallback;
public void setCartCallback(CartCallback cartCallback) {
this.cartCallback = cartCallback;
}
public ProductAdpter(Context context, List<CartBean.Cart.Product> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public ProductAdpter.MyVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=LayoutInflater.from(context).inflate(R.layout.product_item,viewGroup,false);
MyVH myVH = new MyVH(view);
return myVH;
}
@Override
public void onBindViewHolder(@NonNull final MyVH myVH, int i) {
final CartBean.Cart.Product product = list.get(i);
myVH.pd_checkbox.setChecked(product.isProductCheched);
myVH.pd_name.setText(product.title);
myVH.pd_price.setText(product.price+"");
//加载图片
String[] images=product.images.split("\\|");
if (images!=null&&images.length>0){
Glide.with(context).load(images[0]).into(myVH.pd_image);
}
myVH.addminus.setOnAddMinusClick(new AddMinusView.onAddMinusClick() {
@Override
public void onClickNum(int num) {
product.productNum=num;
//重新计算总价
if (cartCallback!=null){
cartCallback.notifyNum();
}
}
});
//点击监听
myVH.pd_checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
product.isProductCheched=myVH.pd_checkbox.isChecked();
if (cartCallback!=null){
for (CartBean.Cart.Product product1 : list) {
if (product1.isProductCheched==false){
cartCallback.notifyItem(false,product1.pos);
return;
}
cartCallback.notifyItem(true,product1.pos);
}
}
}
});
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
public class MyVH extends RecyclerView.ViewHolder {
private final CheckBox pd_checkbox;
private final TextView pd_name,pd_price;
private ImageView pd_image;
private AddMinusView addminus;
public MyVH(@NonNull View itemView) {
super(itemView);
pd_name = itemView.findViewById(R.id.pd_name);
addminus = itemView.findViewById(R.id.addminus);
pd_image = itemView.findViewById(R.id.pd_image);
pd_price = itemView.findViewById(R.id.pd_price);
pd_checkbox = itemView.findViewById(R.id.pd_checkbox);
}
}
}
父`
import java.util.List;
public class CartAdpter extends XRecyclerView.Adapter<CartAdpter.MyVH> implements CartCallback {
private Context context;
private List<CartBean.Cart> list;
private CratUICallback cartUiCallback;
public void setCartUiCallback(CratUICallback cartUiCallback) {
this.cartUiCallback = cartUiCallback;
}
public CartAdpter(Context context, List<CartBean.Cart> list) {
this.context = context;
this.list = list;
}
//更新数据
public void AddList(List<CartBean.Cart> list) {
if (list!=null){
this.list.addAll(list);
}
notifyDataSetChanged();
}
@NonNull
@Override
public CartAdpter.MyVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=LayoutInflater.from(context).inflate(R.layout.cart_item,viewGroup,false);
MyVH myVH = new MyVH(view);
return myVH;
}
@Override
public void onBindViewHolder(@NonNull final CartAdpter.MyVH myVH, int i) {
final CartBean.Cart cart = list.get(i);
myVH.name.setText(cart.sellerName);
myVH.ct_checkbox.setChecked(cart.isCartCheched);
ProductAdpter productAdpter = new ProductAdpter(context, cart.list);
productAdpter.setCartCallback(this);
myVH.recy.setLayoutManager(new LinearLayoutManager(context));
myVH.recy.setAdapter(productAdpter);
//复选框
myVH.ct_checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cart.isCartCheched=myVH.ct_checkbox.isChecked();
for (CartBean.Cart.Product product : cart.list) {
product.isProductCheched=cart.isCartCheched;
return;
}
notifyDataSetChanged();
if (cartUiCallback!=null){
cartUiCallback.notifyCart();
}
}
});
for (CartBean.Cart.Product product : cart.list) {
product.pos=i;
}
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
@Override
public void notifyItem(boolean isChecked, int pos) {
list.get(pos).isCartCheched=isChecked;
notifyDataSetChanged();
if (cartUiCallback!=null){
cartUiCallback.notifyCart();
}
}
@Override
public void notifyNum() {
if (cartUiCallback!=null){
cartUiCallback.notifyCart();
}
}
public class MyVH extends RecyclerView.ViewHolder {
private final CheckBox ct_checkbox;
private final TextView name;
private final XRecyclerView recy;
public MyVH(@NonNull View itemView) {
super(itemView);
recy = itemView.findViewById(R.id.rv);
name = itemView.findViewById(R.id.name);
ct_checkbox = itemView.findViewById(R.id.ct_checkbox);
}
}
}