展示页面
public class ShouFragment extends Fragment implements ContarInterfac.shouFragmentFace{
ContarInterfac.PresenterFara presenterFara;
TextView text_zong;
RecyclerView recyclerview_1;
public CheckBox checkBox_1;
WaiAdapter adapter;
public List<MyBean.DataBean> list = new ArrayList<>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.shou_fragment,container,false);
text_zong=view.findViewById(R.id.text_zong);
recyclerview_1=view.findViewById(R.id.recyclerview_1);
checkBox_1=view.findViewById(R.id.checkbox_1);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerview_1.setLayoutManager(layoutManager);
adapter = new WaiAdapter(list,getContext(),this);
recyclerview_1.setAdapter(adapter);
presenterFara =new MyPersenter<>(this);
presenterFara.getDate();
checkBox_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkBox_1.isChecked()){
ischeke(true);
}else{
ischeke(false);
}
}
});
}
@Override
public void showDate(Object obj) {
MyBean myBean = (MyBean) obj;
list.addAll(myBean.getData());
adapter.notifyDataSetChanged();
}
//点击商家里面的checkbox
public int setchchiischedall(boolean ischeckd,Object o){
MyBean.DataBean.ListBean listBean = (MyBean.DataBean.ListBean) o;
int n =0;
for (int i = 0; i <list.size() ; i++) {
for (int j = 0; j <list.get(i).getList().size() ; j++) {
MyBean.DataBean.ListBean listBean1 =list.get(i).getList().get(j);
if(listBean1.equals(listBean)){
listBean1.lichecde=ischeckd;
n=i;
}
}
}
adapter.notifyDataSetChanged();
setprice();
return n;
}
//点击商家的checkbox
public void setischedall(int i,int tag,boolean ischeckd){
list.get(i).waichecde=ischeckd;
if(tag!=-1){
list.get(i).getList().get(tag).lichecde=false;
}else{
for (int j = 0; j <list.get(i).getList().size() ; j++) {
list.get(i).getList().get(j).lichecde=ischeckd;
}
}
setprice();
adapter.notifyDataSetChanged();
}
//获取是否选中
public void ischeke(boolean b){
for (int i = 0; i <list.size(); i++) {
list.get(i).waichecde=b;
for (int j = 0; j <list.get(i).getList().size() ; j++) {
list.get(i).getList().get(j).lichecde=b;
}
}
adapter.notifyDataSetChanged();
setprice();
}
//计算总价
public void setprice(){
int sum = 0;
for (int i = 0; i <list.size() ; i++) {
for (int j = 0; j <list.get(i).getList().size() ; j++) {
if(list.get(i).getList().get(j).lichecde){
int num = list.get(i).getList().get(j).getNum();
double price = list.get(i).getList().get(j).getPrice();
sum+=price*num;
}
}
}
text_zong.setText(sum+"");
}
}
外面的recyclerview展示
public class WaiAdapter extends RecyclerView.Adapter<WaiAdapter.Holder_1>{
List<MyBean.DataBean> list;
Context context;
ShouFragment shouFragment;
public WaiAdapter(List<MyBean.DataBean> list, Context context,ShouFragment shouFragment) {
this.list = list;
this.context = context;
this.shouFragment=shouFragment;
}
@NonNull
@Override
public Holder_1 onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view =LayoutInflater.from(context).inflate(R.layout.wai_layout,null);
return new Holder_1(view);
}
@Override
public void onBindViewHolder(@NonNull final Holder_1 holder_1, final int i) {
String neme = list.get(i).getSellerName();
holder_1.text_name.setText(neme);
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
holder_1.recyclerview_2.setLayoutManager(layoutManager);
LiAdaoter adapter = new LiAdaoter(list.get(i).getList(), context,shouFragment );
holder_1.recyclerview_2.setAdapter(adapter);
holder_1.checkbox_2.setChecked(list.get(i).waichecde);
holder_1.checkbox_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shouFragment.setischedall(i,-1,holder_1.checkbox_2.isChecked());
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class Holder_1 extends RecyclerView.ViewHolder{
CheckBox checkbox_2;
TextView text_name;
RecyclerView recyclerview_2;
public Holder_1(@NonNull View itemView) {
super(itemView);
checkbox_2=itemView.findViewById(R.id.checkbox_2);
text_name=itemView.findViewById(R.id.text_name);
recyclerview_2=itemView.findViewById(R.id.recyclerview_2);
}
}
}
里面的recyclerview展示
public class LiAdaoter extends RecyclerView.Adapter<LiAdaoter.Holder_2> {
List<MyBean.DataBean.ListBean> list;
Context context;
ShouFragment shouFragment;
public LiAdaoter(List<MyBean.DataBean.ListBean> list, Context context, ShouFragment shouFragment) {
this.list = list;
this.context = context;
this.shouFragment = shouFragment;
}
@NonNull
@Override
public Holder_2 onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view =LayoutInflater.from(context).inflate(R.layout.li_layout,null);
return new Holder_2(view);
}
@Override
public void onBindViewHolder(@NonNull final Holder_2 holder_2, final int i) {
double price = list.get(i).getPrice();
String title = list.get(i).getTitle();
String url = list.get(i).getImages();
final int num = list.get(i).getNum();
holder_2.text_shu.setText(num+"");
holder_2.text_title.setText(title);
holder_2.text_price.setText(price+"");
Glide.with(context).load(url).into(holder_2.image_view);
holder_2.checkbox_3.setChecked(list.get(i).lichecde);
holder_2.checkbox_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(holder_2.checkbox_3.isChecked()==false){
int n = shouFragment.setchchiischedall(holder_2.checkbox_3.isChecked(),list.get(i));
shouFragment.setischedall(n,i,false);
shouFragment.checkBox_1.setChecked(false);
}else{
int n = shouFragment.setchchiischedall(holder_2.checkbox_3.isChecked(),list.get(i));
}
}
});
holder_2.text_jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num1 = list.get(i).getNum();
num1++;
holder_2.text_shu.setText(num1+"");
list.get(i).setNum(num1);
shouFragment.setprice();
}
});
holder_2.text_jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num1 = list.get(i).getNum();
if(num1==1){
num1=1;
}else{
num1--;
holder_2.text_shu.setText(num1+"");
list.get(i).setNum(num1);
shouFragment.setprice();
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class Holder_2 extends RecyclerView.ViewHolder{
ImageView image_view;
TextView text_jia,text_jian,text_shu,text_title,text_price;
CheckBox checkbox_3;
public Holder_2(@NonNull View itemView) {
super(itemView);
image_view =itemView.findViewById(R.id.image_view);
text_jia =itemView.findViewById(R.id.text_jia);
text_jian =itemView.findViewById(R.id.text_jian);
text_shu =itemView.findViewById(R.id.text_shu);
text_title =itemView.findViewById(R.id.text_title);
text_price =itemView.findViewById(R.id.text_price);
checkbox_3 =itemView.findViewById(R.id.checkbox_3);
}
}
}