购物车页面及代码

 
  
子类适配器代码



    

    
        
    

    

        

        

        
    
父类适配器代码



 
     

     
 

    

java适配器代码
package com.example.lenovo.moniyuekao.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.lenovo.moniyuekao.R;
import com.example.lenovo.moniyuekao.bean.BeanCar;
import com.example.lenovo.moniyuekao.bean.ShopEvent;
import com.example.lenovo.moniyuekao.ui.AddSubView;
import com.facebook.drawee.view.SimpleDraweeView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;


/**
 * date:2018/6/29 10:38

 * function:
 */
public class FragmentAdapterCar extends RecyclerView.Adapter {
    private Context context;
    private List data;


    public FragmentAdapterCar(Context context, List data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.carpresent, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.mTextCarShop.setText(data.get(position).getSellerName());
        ChildAdapterCar childAdapterCar = new ChildAdapterCar(context, data.get(position).getList());
        viewHolder.mRecyCarPresent.addItemDecoration(new DividerItemDecoration(context,DividerItemDecoration.VERTICAL));
        viewHolder.mRecyCarPresent.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        viewHolder.mRecyCarPresent.setAdapter(childAdapterCar);
        //商家选择子类
        viewHolder.mCheckCarChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean checked = viewHolder.mCheckCarChild.isChecked();

                if (mOnClickShopItem!=null){
                    mOnClickShopItem.OnShopclick(position,checked);
                }
                EventBus.getDefault().post(new ShopEvent(checked, position,data));
            }
        });

        /**
         *复选框的状态值
         */
        List list = data.get(position).getList();
        int si=0;
        for (int i = 0; i < list.size(); i++) {
            si+=list.get(i).getSelected();
        }
        if (si==list.size()){
            viewHolder.mCheckCarChild.setChecked(true);
        }else {
            viewHolder.mCheckCarChild.setChecked(false);
        }

    }


    @Override
    public int getItemCount() {
        return data.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        private CheckBox mCheckCarChild;
        private TextView mTextCarShop;
        private RecyclerView mRecyCarPresent;

        public ViewHolder(View itemView) {
            super(itemView);
            mCheckCarChild = itemView.findViewById(R.id.check_car_child);
            mTextCarShop = itemView.findViewById(R.id.text_car_shop);
            mRecyCarPresent = itemView.findViewById(R.id.recy_car_present);
        }
    }

    /**
     * 内部类
     */
    class ChildAdapterCar extends RecyclerView.Adapter {

        private Context context;
        private List list;


        public ChildAdapterCar(Context context, List list) {
            this.context = context;
            this.list = list;
            if (!EventBus.getDefault().isRegistered(this)){
                EventBus.getDefault().register(this);
            }
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.carchild, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
            final ViewHolder holder1 = (ViewHolder) holder;
            String[] split = list.get(position).getImages().split("\\|");
            holder1.mImageCarChild.setImageURI(Uri.parse(split[0]));
            int selected = list.get(position).getSelected();
            if (selected==1){
                holder1.mCheckCarChild.setChecked(true);
            }else {
                holder1.mCheckCarChild.setChecked(false);
            }
            holder1.mTextChild.setText(list.get(position).getTitle());
            holder1.mTextPrice.setText("¥"+list.get(position).getPrice());
            holder1.addsub.setTextnum(list.get(position).getNum());
/**
 * 复选框
 */
            holder1.mCheckCarChild.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean checked = holder1.mCheckCarChild.isChecked();

                if (mOnClickCheckItem!=null){
                    mOnClickCheckItem.onClickCheck(checked,position,list);
                }
                }
            });
/**
 * 加减其
 */
holder1.addsub.setmOnAddItemClick(new AddSubView.OnAddItemClick() {
    @Override
    public void OnAddClick() {
        holder1.mCheckCarChild.setChecked(true);
        int textnum = holder1.addsub.getTextnum();
        if (mOnClickAddSub!=null){
            mOnClickAddSub.onClickAdd(textnum,list,position);
        }
    }
});

        }

        @Override
        public int getItemCount() {
            return list.size();
        }

@Subscribe(threadMode = ThreadMode.MAIN)
public void getShopCheck(ShopEvent shopEvent){
    int position = shopEvent.getPosition();
    List list = shopEvent.getData().get(position).getList();
//商家选择子类
    //修改网络的数据
    
    if (shopEvent.getCheck()){
                for (int i = 0; i  list);
    }

    private OnClickCheckItem mOnClickCheckItem;

    public void setmOnClickCheckItem(OnClickCheckItem mOnClickCheckItem) {
        this.mOnClickCheckItem = mOnClickCheckItem;
    }
    //加减其
    public interface OnClickAddSub{
        void onClickAdd(int i, List list, int textnum);
    }

    private OnClickAddSub mOnClickAddSub;

    public void setmOnClickAddSub(OnClickAddSub mOnClickAddSub) {
        this.mOnClickAddSub = mOnClickAddSub;
    }
}
逻辑代码
package com.example.lenovo.moniyuekao.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lenovo.moniyuekao.R;
import com.example.lenovo.moniyuekao.adapter.FragmentAdapterCar;
import com.example.lenovo.moniyuekao.bean.BeanCar;
import com.example.lenovo.moniyuekao.bean.BeanCarUpData;
import com.example.lenovo.moniyuekao.utils.MySession;
import com.example.lenovo.moniyuekao.utils.NetUtils;

import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * date:2018/6/29 10:06

 * function:
 */
public class FragmentCar extends Fragment {
    private RecyclerView mRecyCar;
    private CheckBox mCarCheck;
    private TextView mTextCar;
    private TextView mJiesuan;
    private MySession api;
    double sum;
    private Observable dataCar;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_car, container, false);
        initView(view);

        //数据
        MySession mySession = NetUtils.getNetUtils("https://www.zhaoapi.cn/").mySessionApi();
        dataCar = mySession.getDataCar("15768");
//修改数据库
        api = NetUtils.getNetUtils("https://www.zhaoapi.cn/").mySessionApi();

        //查询数据库 赋值
        selectCar(dataCar);
        selectSun(dataCar);
       // checkAll(dataCar);
        return view;
    }

    //全选
    private void checkAll(Observable dataCar) {
        dataCar.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1() {
                    @Override
                    public void call(BeanCar beanCar) {
                        List beanCarData = beanCar.getData();
                        if (mCarCheck.isChecked()){
                            for (int i = 0; i < beanCarData.size(); i++) {
                                for (int j = 0; j < beanCarData.get(i).getList().size(); j++) {

                                    Observable upCar = api.getUpCar("15768",  beanCarData.get(i).getList().get(j).getSellerid() + "", beanCarData.get(i).getList().get(j).getPid() + "", "1", beanCarData.get(i).getList().get(j).getNum() + "");
                                    upCar.subscribeOn(Schedulers.io())
                                            .observeOn(AndroidSchedulers.mainThread())
                                            .subscribe(new Action1() {
                                                @Override
                                                public void call(BeanCarUpData beanCarUpData) {

                                                }
                                            });

                                }
                            }
                        }else {
                            for (int i = 0; i < beanCarData.size(); i++) {
                                for (int j = 0; j < beanCarData.get(i).getList().size(); j++) {

                                    Observable upCar = api.getUpCar("15768",  beanCarData.get(i).getList().get(j).getSellerid() + "", beanCarData.get(i).getList().get(j).getPid() + "", "0", beanCarData.get(i).getList().get(j).getNum() + "");
                                    upCar.subscribeOn(Schedulers.io())
                                            .observeOn(AndroidSchedulers.mainThread())
                                            .subscribe(new Action1() {
                                                @Override
                                                public void call(BeanCarUpData beanCarUpData) {

                                                }
                                            });

                                }
                            }
                        }
                    }
                });
    }

    //Rxjava 总价
    private void selectSun(final Observable dataCar){
       mTextCar.setText(0+"");
       sum=0;
        dataCar.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1() {
                    @Override
                    public void call(BeanCar beanCar) {
                        List beanCarData = beanCar.getData();
                        for (int i = 0; i < beanCarData.size(); i++) {
                            for (int j = 0; j < beanCarData.get(i).getList().size(); j++) {
                                if (beanCarData.get(i).getList().get(j).getSelected()==1){
                                    double v = beanCarData.get(i).getList().get(j).getNum() * beanCarData.get(i).getList().get(j).getPrice();
                                    sum+=v;
                                    mTextCar.setText(sum+"");
                                }
                            }
                        }

                    }
                });
    }




    private void selectCar(final Observable dataCar) {
        dataCar.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1() {
                    @Override
                    public void call(BeanCar beanCar) {
                        final List data = beanCar.getData();
                        FragmentAdapterCar fragmentAdapterCar = new FragmentAdapterCar(getActivity(),data);
                        mRecyCar.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
                        mRecyCar.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
                        mRecyCar.setAdapter(fragmentAdapterCar);
//https://www.zhaoapi.cn/product/updateCarts?uid=71&sellerid=1&pid=1&selected=0&num=10
                        /**
                         * 点击商品
                         */

                        fragmentAdapterCar.setmOnClickLeftItem(new FragmentAdapterCar.OnClickShopItem() {
                            @Override
                            public void OnShopclick(int position, boolean checked) {
                               // Toast.makeText(getActivity(), "++", Toast.LENGTH_SHORT).show();
                                BeanCar.DataBean dataBean = data.get(position);
                                /**
                                 * 修改
                                 */
                                List list = dataBean.getList();
                               if (checked){
                                   for (int i = 0; i  upCar = api.getUpCar("15768", list.get(i).getSellerid() + "", list.get(i).getPid() + "", "1", list.get(i).getNum() + "");
                                       upCar.subscribeOn(Schedulers.io())
                                               .observeOn(AndroidSchedulers.mainThread())
                                               .subscribe(new Action1() {
                                                   @Override
                                                   public void call(BeanCarUpData beanCarUpData) {
                                                       Toast.makeText(getActivity(), beanCarUpData.getMsg(), Toast.LENGTH_SHORT).show();

                                                   }
                                               });
                                   }
                               }else {
                                   for (int i = 0; i  upCar = api.getUpCar("15768",list.get(i).getSellerid()+"",list.get(i).getPid()+"","0",list.get(i).getNum()+"");
                                       upCar.subscribeOn(Schedulers.io())
                                               .observeOn(AndroidSchedulers.mainThread())
                                               .subscribe(new Action1() {
                                                   @Override
                                                   public void call(BeanCarUpData beanCarUpData) {
                                                       Toast.makeText(getActivity(), beanCarUpData.getMsg(), Toast.LENGTH_SHORT).show();
                                                   }
                                               });
                                  }
                               }
                                selectSun(dataCar);

                            }
                        });


                        /*
                        子的选选择
                         */
                    fragmentAdapterCar.setmOnClickCheckItem(new FragmentAdapterCar.OnClickCheckItem() {
                        @Override
                        public void onClickCheck(boolean checked, int position, List list) {
                            int select=0;
                            if (checked){
                               select=1;
                            }else {
                                select=0;
                            }
                            Observable upCar = api.getUpCar("15768",list.get(position).getSellerid()+"",list.get(position).getPid()+"",select+"",list.get(position).getNum()+"");
                            upCar.subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Action1() {
                                        @Override
                                        public void call(BeanCarUpData beanCarUpData) {
                                            selectCar(dataCar);
                                            Toast.makeText(getActivity(), beanCarUpData.getMsg(), Toast.LENGTH_SHORT).show();
                                        }
                                    });
                            selectSun(dataCar);

                        }
                    });

                        /**
                         * 加减其
                         */
                        fragmentAdapterCar.setmOnClickAddSub(new FragmentAdapterCar.OnClickAddSub() {
                            @Override
                            public void onClickAdd(int textnum, List list, int i) {
                                Observable upCar = api.getUpCar("15768",list.get(i).getSellerid()+"",list.get(i).getPid()+"",1+"",textnum+"");
                                upCar.subscribeOn(Schedulers.io())
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .subscribe(new Action1() {
                                            @Override
                                            public void call(BeanCarUpData beanCarUpData) {
                                                selectCar(dataCar);
                                                Toast.makeText(getActivity(), beanCarUpData.getMsg(), Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                selectSun(dataCar);

                            }
                        });



                    }
                });
    }



    private void initView(View view) {
        mRecyCar = view.findViewById(R.id.recy_car);
        mCarCheck = view.findViewById(R.id.car_check);
        mTextCar = view.findViewById(R.id.text_car);
        mJiesuan = view.findViewById(R.id.jiesuan);
    }
}


你可能感兴趣的:(购物车页面及代码)