public class ShopCarAdapter extends RecyclerView.Adapter<ShopCarAdapter.ViewHolder> {
private Context context;
private List list =new ArrayList<>();
public ShopCarAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.gwc_item, null, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
final ShoppingBean shopCarBean = list.get(i);
viewHolder.checkBox.setText(list.get(i).categoryName);
viewHolder.adapter.clear();
viewHolder.adapter.add(shopCarBean.shoppingCartList);
viewHolder.adapter.notifyDataSetChanged();
viewHolder.recyclerView.setAdapter(viewHolder.adapter);
viewHolder.checkBox.setChecked(shopCarBean.isChecked);
viewHolder.checkBox.setTag(shopCarBean);
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<ShoppingChildBean> shoppingCartList = list.get(i).shoppingCartList;
ShoppingBean viewTag = (ShoppingBean) v.getTag();
CheckBox checkBox = (CheckBox) v;
if (checkBox.isChecked()){
viewTag.isChecked=true;
for (int j = 0; j < shoppingCartList.size(); j++) {
shoppingCartList.get(j).check=checkBox.isChecked();
}
viewHolder.adapter.notifyDataSetChanged();
}else {
viewTag.isChecked=false;
for (int j = 0; j < shoppingCartList.size(); j++) {
shoppingCartList.get(j).check=checkBox.isChecked();
}
viewHolder.adapter.notifyDataSetChanged();
}
priceAll();
}
});
//判断二级条目中是否全部选中,如果全选中 则让一级的Checkbox选中,否则相反
viewHolder.adapter.setShoppingData(new ShoppingRecyclerAdapter.ShoppingData() {
@Override
public void getData(int a) {
if (a>list.get(i).shoppingCartList.size()){
list.get(i).isChecked=true;
}else {
list.get(i).isChecked=false;
}
priceAll();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
//全选反选
public void checkedAll(boolean isChecked){
for (int i = 0; i <list.size() ; i++) {
list.get(i).isChecked=isChecked;
final ShoppingBean shopCarBean = list.get(i);
for (int j = 0; j < list.get(i).shoppingCartList.size(); j++) {
shopCarBean.shoppingCartList.get(j).check =isChecked;
}
}
notifyDataSetChanged();
}
//计算价格
public void priceAll() {
int a=0;
int b=0;
double num = 0.0;
for (int i = 0; i <list.size() ; i++) {
final ShoppingBean shopCarBean = list.get(i);
for (int j = 0; j < list.get(i).shoppingCartList.size(); j++) {
b++;
ShoppingChildBean shoppingChildBean = shopCarBean.shoppingCartList.get(j);
if (shoppingChildBean.check) {
num += shoppingChildBean.price * shoppingChildBean.count;
a++;
}
}
}
if (a>=b){
dataCall.dataCall(true);
}else {
dataCall.dataCall(false);
}
call.mCall(num,a);
//a=0;
notifyDataSetChanged();
}
//暴露方法
public List<ShoppingBean> getlist(){
return list;
}
public void clear() {
list.clear();
}
public void addList(List<ShoppingBean> result) {
if (result!=null){
list.addAll(result);
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final CheckBox checkBox;
private final RecyclerView recyclerView;
ShoppingRecyclerAdapter adapter;
public ViewHolder(@NonNull View itemView) {
super(itemView);
checkBox = itemView.findViewById(R.id.check);
recyclerView = itemView.findViewById(R.id.child_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
adapter = new ShoppingRecyclerAdapter(context);
}
}
private Call call;
public void setCall(Call call) {
this.call = call;
}
public interface Call{
void mCall(double a, int b);
}
private DataCall dataCall;
public void setDataCall(DataCall dataCall) {
this.dataCall = dataCall;
}
public interface DataCall{
void dataCall(boolean a);
}
}