fragmenter
public class Shou1Fragmenter extends Fragment implements IManintenter, ZongJiaintenter {
@BindView(R.id.quanxuan)
CheckBox quanxuan;
@BindView(R.id.zongjia)
TextView zongjia;
@BindView(R.id.shangpinRecyclerView)
RecyclerView shangpinRecyclerView;
Unbinder unbinder;
private ShangPinAdapter pinAdapter;
private List<GouWuChBean.DataBean> data;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shou1layout, container, false);
unbinder = ButterKnife.bind(this, view);
if (getUserVisibleHint()) {
GouWuChePager gouWuChePager = new GouWuChePager(this);
gouWuChePager.getgouwuchepresenter();
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
manager.setOrientation(LinearLayoutManager.VERTICAL);
shangpinRecyclerView.setLayoutManager(manager);
pinAdapter = new ShangPinAdapter(getActivity(),this);
shangpinRecyclerView.setAdapter(pinAdapter);
}
return view;
}
@Override
public void getintenter(GouWuChBean gouWuChBean) {
data = gouWuChBean.getData();
pinAdapter.setData(data);
pinAdapter.setQuanxuan(quanxuan);
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@OnClick(R.id.quanxuan)
public void onViewClicked() {
boolean checked = quanxuan.isChecked();
for (int i = 0; i <data.size(); i++) {
data.get(i).setClick(checked);
List<GouWuChBean.DataBean.ListBean> list = data.get(i).getList();
for (int j = 0; j <list.size() ; j++) {
list.get(j).setClick(checked);
}
}
getzongjia();
pinAdapter.notifyDataSetChanged();
}
private void getzongjia(){
double zong=0;
for (int i = 0; i <data.size() ; i++) {
List<GouWuChBean.DataBean.ListBean> list = data.get(i).getList();
for (int j = 0; j <list.size() ; j++) {
boolean click = list.get(j).isClick();
if (click) {
String num = list.get(j).getNum();
String price = list.get(j).getPrice();
int i1 = Integer.parseInt(num);
double v = Double.parseDouble(price);
zong += i1 * v;
}
}
}
zongjia.setText("总价为:¥"+zong+"");
}
@Override
public void zong() {
getzongjia();
}
}
*
商家adapter
public class ShangPinAdapter extends RecyclerView.Adapter<ShangPinAdapter.MMA> {
Context context;
LayoutInflater inflater;
private ZiAdapter adapter;
private ZongJiaintenter zongJiaintenter;
public ShangPinAdapter(Context contexts, ZongJiaintenter zongJiaintenters) {
this.zongJiaintenter = zongJiaintenters;
this.context = contexts;
this.inflater = LayoutInflater.from(context);
}
private CheckBox quanxuan;
public void setQuanxuan(CheckBox quanxuan) {
this.quanxuan = quanxuan;
}
@NonNull
@Override
public MMA onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = inflater.inflate(R.layout.gouwuchelayout, viewGroup, false);
MMA mma = new MMA(view);
return mma;
}
@Override
public void onBindViewHolder(@NonNull MMA mma, final int i) {
String sellerName = data.get(i).getSellerName();
mma.funame.setText(sellerName);
boolean click = data.get(i).isClick();
mma.fuclick.setChecked(click);
LinearLayoutManager manager = new LinearLayoutManager(context);
manager.setOrientation(LinearLayoutManager.VERTICAL);
mma.ziRecyclerView.setLayoutManager(manager);
adapter = new ZiAdapter(context,zongJiaintenter);
adapter.setFuclick(mma.fuclick, data);
adapter.setQuanxuan(quanxuan);
mma.ziRecyclerView.setAdapter(adapter);
final List<GouWuChBean.DataBean.ListBean> list = data.get(i).getList();
adapter.setData(list);
mma.fuclick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox box = (CheckBox) v;
boolean checked = box.isChecked();
data.get(i).setClick(checked);
for (int j = 0; j < list.size(); j++) {
list.get(j).setClick(checked);
}
boolean quabxuanss = quabxuanss();
quanxuan.setChecked(quabxuanss);
zongJiaintenter.zong();
notifyDataSetChanged();
}
});
}
private boolean quabxuanss() {
for (int i = 0; i < data.size(); i++) {
boolean click = data.get(i).isClick();
if (!click) {
return false;
}
}
return true;
}
@Override
public int getItemCount() {
return data.size();
}
List<GouWuChBean.DataBean> data = new ArrayList<>();
public void setData(List<GouWuChBean.DataBean> datas) {
this.data.clear();
this.data.addAll(datas);
notifyDataSetChanged();
}
public class MMA extends RecyclerView.ViewHolder {
private final CheckBox fuclick;
private final TextView funame;
private final RecyclerView ziRecyclerView;
public MMA(@NonNull View itemView) {
super(itemView);
fuclick = itemView.findViewById(R.id.fuclick);
funame = itemView.findViewById(R.id.funame);
ziRecyclerView = itemView.findViewById(R.id.ziRecyclerView);
}
}
}
商品adapter
public class ZiAdapter extends RecyclerView.Adapter<ZiAdapter.BBA> {
Context context;
LayoutInflater inflater;
private ZongJiaintenter zongJiaintenter;
public ZiAdapter(Context context1,ZongJiaintenter zongJiaintenters) {
this.zongJiaintenter=zongJiaintenters;
this.context = context1;
this.inflater = LayoutInflater.from(context);
}
private CheckBox quanxuan;
public void setQuanxuan(CheckBox quanxuan) {
this.quanxuan = quanxuan;
}
private CheckBox fuclick;
private List<GouWuChBean.DataBean> listBeans;
public void setFuclick(CheckBox fuclick,List<GouWuChBean.DataBean> beanList) {
this.listBeans=beanList;
this.fuclick = fuclick;
}
@NonNull
@Override
public BBA onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = inflater.inflate(R.layout.zilayout, viewGroup, false);
BBA bba = new BBA(view);
return bba;
}
@Override
public void onBindViewHolder(@NonNull BBA bba, final int i) {
String price = list.get(i).getPrice();
bba.zijiage.setText("¥" + price);
String title = list.get(i).getTitle();
bba.ziname.setText(title);
Uri uri = Uri.parse(list.get(i).getImages());
bba.ziimg.setImageURI(uri);
boolean click = list.get(i).isClick();
bba.ziclick.setChecked(click);
bba.ziclick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox box= (CheckBox) v;
boolean checked = box.isChecked();
list.get(i).setClick(checked);
boolean fuclicks = fuclicks();
fuclick.setChecked(fuclicks);
boolean quanxuans = quanxuans();
quanxuan.setChecked(quanxuans);
zongJiaintenter.zong();
notifyDataSetChanged();
}
});
}
private boolean quanxuans(){
for (int i = 0; i <listBeans.size(); i++) {
List<GouWuChBean.DataBean.ListBean> list = listBeans.get(i).getList();
for (int j = 0; j < list.size(); j++) {
boolean click = list.get(j).isClick();
if (!click) {
return false;
}
}
}
return true;
}
private boolean fuclicks() {
for (int i = 0; i < list.size(); i++) {
boolean click = list.get(i).isClick();
if (!click) {
return false;
}
}
return true;
}
@Override
public int getItemCount() {
return list.size();
}
List<GouWuChBean.DataBean.ListBean> list = new ArrayList<>();
public void setData(List<GouWuChBean.DataBean.ListBean> lists) {
this.list.clear();
this.list.addAll(lists);
notifyDataSetChanged();
}
public class BBA extends RecyclerView.ViewHolder {
private final TextView zijiage;
private final TextView ziname;
private final SimpleDraweeView ziimg;
private final CheckBox ziclick;
public BBA(@NonNull View itemView) {
super(itemView);
zijiage = itemView.findViewById(R.id.zijiage);
ziname = itemView.findViewById(R.id.ziname);
ziimg = itemView.findViewById(R.id.ziSimpleDraweeView);
ziclick = itemView.findViewById(R.id.zi);
}
}
}
全选接口
public interface ZongJiaintenter {
void zong();
}