public class FragmentTwo extends Fragment implements ShowContract.ShowView,NotfyNum {
private TextView fan;
private XRecyclerView xr;
private CheckBox checkbox;
private TextView zong;
private Button jies;
private ShowPresenter showPresenter;
private List<ShowBean.DataBase> datas;
private YAdapter yAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentwo, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView(view);
}
@Override
public void success(String reslut) {
if (!reslut.equals("null")){
for (ShowBean.DataBase data : datas) {
for (ShowBean.DataBase.Product product : data.list) {
product.productNum=1;
}
}
ShowBean showBean = new Gson().fromJson(reslut, ShowBean.class);
datas = showBean.data;
//创建适配器
yAdapter = new YAdapter(getActivity());
xr.setLayoutManager(new LinearLayoutManager(getActivity()));
xr.setAdapter(yAdapter);
yAdapter.setList(datas);
yAdapter.setNotfyNum(this);
}
}
@Override
public void successadd(String reslut) {
}
@Override
public void failure(String error) {
}
private void initView(View view) {
fan = (TextView)view.findViewById(R.id.fan);
xr = (XRecyclerView)view. findViewById(R.id.xr);
//刷新
xr.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
reqData();
xr.refreshComplete();
}
@Override
public void onLoadMore() {
}
});
checkbox = (CheckBox)view. findViewById(R.id.checkbox);
zong = (TextView)view. findViewById(R.id.zong);
jies = (Button)view. findViewById(R.id.jies);
reqData();
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
for (ShowBean.DataBase dataBase : datas) {
dataBase.checkbox=true;
for (ShowBean.DataBase.Product product : dataBase.list) {
product.isproductcheckbox=true;
}
}
}else {
for (ShowBean.DataBase dataBase : datas) {
dataBase.checkbox=false;
for (ShowBean.DataBase.Product product : dataBase.list) {
product.isproductcheckbox=false;
}
}
}
yAdapter.notifyDataSetChanged();
//计算总价
getprice();
}
});
}
//计算总价
private void getprice() {
double zj=0.0;
for (ShowBean.DataBase data : datas) {
for (ShowBean.DataBase.Product product : data.list) {
if (product.isproductcheckbox){
zj+=product.price*product.productNum;
}
}
}
zong.setText("¥:"+zj);
}
//数据
private void reqData(){
showPresenter = new ShowPresenter(this);
datas=new ArrayList<>();
HashMap<String,String>map=new HashMap<>();
map.put("uid","72");
showPresenter.showcha(Api.SELTE,map);
}
@Override
public void notifynum() {
getprice();
}
@Override
public void onDestroy() {
super.onDestroy();
if (showPresenter!=null){
showPresenter.setcacle();
}
}
}
一级适配
public class YAdapter extends XRecyclerView.Adapter<YAdapter.YViewHolder> implements ShCheckbox {
/*setLayoutManager(new GridLayoutManager(context,2,GridLayoutManager.HORIZONTAL,false));*/
private Context context;
private List<ShowBean.DataBase> list;
private NotfyNum notfyNum;
public YAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void setList(List<ShowBean.DataBase> list) {
if (list!=null){
this.list = list;
}
notifyDataSetChanged();
}
public void setNotfyNum(NotfyNum notfyNum) {
this.notfyNum = notfyNum;
}
@NonNull
@Override
public YViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=LayoutInflater.from(context).inflate(R.layout.item_yj,viewGroup,false);
YViewHolder yViewHolder=new YViewHolder(view);
return yViewHolder;
}
@Override
public void onBindViewHolder(@NonNull final YViewHolder yViewHolder, final int i) {
if (list.size()>0){
//二级列表下标
for (ShowBean.DataBase.Product product : list.get(i).list) {
product.pos=i;
}
yViewHolder.checkbox.setChecked(list.get(i).checkbox);
yViewHolder.name.setText(list.get(i).sellerName);
yViewHolder.xr.setLayoutManager(new LinearLayoutManager(context));
//创建适配器
EAdapter eAdapter = new EAdapter(context, list.get(i).list);
yViewHolder.xr.setAdapter(eAdapter);
eAdapter.setShCheckbox(this);
//点击事件
yViewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.get(i).checkbox=yViewHolder.checkbox.isChecked();
//遍历循环二级列表
for (ShowBean.DataBase.Product product : list.get(i).list) {
product.isproductcheckbox=list.get(i).checkbox;
}
notifyDataSetChanged();
}
});
}
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void notcheck(boolean isCheck, int postion) {
list.get(postion).checkbox=isCheck;
notifynum();
}
@Override
public void notifynum() {
if (notfyNum!=null){
notfyNum.notifynum();
}
}
class YViewHolder extends RecyclerView.ViewHolder {
private TextView name,xiao;
private XRecyclerView xr;
private CheckBox checkbox;
public YViewHolder(@NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.name);
xiao=itemView.findViewById(R.id.xiao);
xr=itemView.findViewById(R.id.xr);
checkbox=itemView.findViewById(R.id.checkbox);
}
}
}
二级适配
public class EAdapter extends XRecyclerView.Adapter<EAdapter.EViewHolder> {
private Context context;
private List<ShowBean.DataBase.Product> list;
private ShCheckbox shCheckbox;
public EAdapter(Context context, List<ShowBean.DataBase.Product> list) {
this.context = context;
this.list = list;
}
public void setShCheckbox(ShCheckbox shCheckbox) {
this.shCheckbox = shCheckbox;
}
@NonNull
@Override
public EViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=LayoutInflater.from(context).inflate(R.layout.item_erj,viewGroup,false);
EViewHolder eViewHolder=new EViewHolder(view);
return eViewHolder;
}
@Override
public void onBindViewHolder(@NonNull final EViewHolder eViewHolder, final int i) {
if (list.size()>0){
eViewHolder.checkbox.setChecked(list.get(i).isproductcheckbox);
eViewHolder.title.setText(list.get(i).title);
eViewHolder.price.setText("¥:"+list.get(i).price);
String[] image=list.get(i).images.split("\\|");
if (image!=null&&image.length>0){
Glide.with(context).load(image[0]).into(eViewHolder.img);
}
eViewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.get(i).isproductcheckbox=eViewHolder.checkbox.isChecked();
shCheckbox.notifynum();
for (ShowBean.DataBase.Product product : list) {
if (!product.isproductcheckbox){
if (shCheckbox!=null){
shCheckbox.notcheck(false,product.pos);
}
return;
}
shCheckbox.notcheck(true,product.pos);
}
}
});
eViewHolder.addView.setAddView(new AddView.AddCallBack() {
@Override
public void numCallback(int num) {
list.get(i).productNum=num;
if (shCheckbox!=null){
shCheckbox.notifynum();
}
}
});
}
}
@Override
public int getItemCount() {
return list.size();
}
class EViewHolder extends RecyclerView.ViewHolder {
private TextView title,price;
private ImageView img;
private CheckBox checkbox;
private AddView addView;
public EViewHolder(@NonNull View itemView) {
super(itemView);
checkbox=itemView.findViewById(R.id.checkbox);
img=itemView.findViewById(R.id.img);
addView=itemView.findViewById(R.id.myview);
title=itemView.findViewById(R.id.title);
price=itemView.findViewById(R.id.price);
}
}
}