public class ShowAdapter extends RecyclerView.Adapter<ShowAdapter.ViewHolder> {
private List<DataBean> list;
private Context context;
public ShowAdapter(List<DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.show_item, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
viewHolder.text.setText(list.get(i).getSellerName());
RecyclerView recycler_view=viewHolder.recycler_view;
final List<ListBean> mlist = list.get(i).getList();
final ListAdapter listAdapter = new ListAdapter(mlist,context);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recycler_view.setLayoutManager(linearLayoutManager);
recycler_view.setAdapter(listAdapter);
//checkbox
DataBean dataBean = list.get(i);
viewHolder.box.setChecked(dataBean.isChecked());
listAdapter.setLisenter(new ListAdapter.ShopListCallBack() {
@Override
public void callBack() {
if (mlisenter!=null){
mlisenter.callBack(list);
}
List<ListBean> listBeans = list.get(i).getList();
boolean isAllchecked=true;
for (ListBean bean:listBeans){
if (!bean.isCheck()){
isAllchecked=false;
break;
}
}
//刷新商家的状态
viewHolder.box.setChecked(isAllchecked);
list.get(i).setChecked(isAllchecked);
}
});
//监听checkBox的点击事件
//目的是改变旗下所有商品的选中状态
viewHolder.box.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//首先改变自己的标志位
list.get(i).setChecked(viewHolder.box.isChecked());
//首先改变自己的标志位
listAdapter.checkOrRemoveAll(viewHolder.box.isChecked());
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public void setList(List<DataBean> mlist){
this.list=mlist;
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final TextView text;
private final CheckBox box;
private final RecyclerView recycler_view;
public ViewHolder(@NonNull View itemView) {
super(itemView);
box = itemView.findViewById(R.id.box_show);
text = itemView.findViewById(R.id.text_show);
recycler_view = itemView.findViewById(R.id.recycler_show);
}
}
private ShopShowCallBack mlisenter;
public void setLisenter(ShopShowCallBack shopCallBack){
this.mlisenter =shopCallBack;
}
public interface ShopShowCallBack{
void callBack(List<DataBean> mlist);
}
}