购物车

一点MainActivity

public class MainActivity extends AppCompatActivity implements ContarInterFace.MyViewInterFace {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        presenterInterFace = new Presenter (this);
        recy();
        init();
    }
    public void recy() {
        recycler = findViewById (R.id.recycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager (this);
        layoutManager.setOrientation (LinearLayoutManager.VERTICAL);
        recycler.setLayoutManager (layoutManager);
        myAdapter = new MyAdapter (this, list);
        recycler.setAdapter (myAdapter);
        presenterInterFace.toModel ();
        text1 = findViewById (R.id.text1);
        checkBox = findViewById (R.id.check);
        checkBox.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View v) {
               //总价
                int sum=0;
                boolean isSelected = checkBox.isChecked ();

                if(isSelected){
                    for (int i=0;i<list.size ();i++){
                        list.get (i).isSelect=isSelected;
                        for (int j=0;j<list.get (i).getList ().size ();j++){
                            list.get(i).getList ().get (j).itemSelect=isSelected;

                            int price = list.get (i).getList ().get (j).getPrice ();
                            int num = list.get (i).getList ().get (j).getNum ();
                            sum+=price*num;
                        }
                    }
                }else{
                    for (int i=0;i<list.size ();i++){
                        list.get (i).isSelect=isSelected;
                        for (int j=0;j<list.get (i).getList ().size ();j++){
                            list.get(i).getList ().get (j).itemSelect=isSelected;
                            sum=0;
                        }
                    }
                }
                text1.setText (sum+"");
                myAdapter.notifyDataSetChanged ();
            }
        });

        myAdapter.setMyCallBack (new MyAdapter.MyCallBack () {
            @Override
            public void setChild(List<Product.DataBean> list) {
                int sums=0;

                int ss=0;
                for (int i=0;i<list.size ();i++){
                    boolean b = list.get (i).isSelect;
                    if(b){
                        ss++;
                        for (int j=0;j<list.get (i).getList ().size ();j++){
                            int price = list.get (i).getList ().get (j).getPrice ();
                            int num = list.get (i).getList ().get (j).sum;
                            sums+=price*num;
                        }
                    }else{
                    for (int j=0;j<list.get (i).getList ().size ();j++) {
                        if (list.get (i).getList ().get (j).itemSelect) {
                            int price = list.get (i).getList ().get (j).getPrice ();
                            int num = list.get (i).getList ().get (j).sum;
                            sums+= price * num;
                        }
                    }
                    }
                }
                if(ss == list.size ()){
                    checkBox.setChecked (true);
                }else{
                    checkBox.setChecked (false);
                }
                text1.setText (sums+"");
                myAdapter.notifyDataSetChanged ();
            }

        });

    }

    public void init() {
        radioGroup = findViewById (R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener (new RadioGroup.OnCheckedChangeListener () {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radio1:
                        break;
                    case R.id.radio2:
                        Intent intent = new Intent (MainActivity.this, SecondActivity.class);
                        startActivity (intent);
                        finish ();
                        break;
                }
            }
        });
    }


    @Override
    public void toShow(Object o) {
            Product product = (Product) o;
            List<Product.DataBean> mlist= product.getData ();
            list.addAll (mlist);
            myAdapter.notifyDataSetChanged ();
    }
}

二 点 P层

public class Presenter<T> implements ContarInterFace.PresenterInterFace {

    Model model;
    T tt;
    ContarInterFace.MyViewInterFace myViewInterFace;
    public Presenter(T t){
        model = new Model ();
        this.tt = t;
    }

    @Override
    public void toModel() {
         model.toRequest (new Model.MyCallBack () {
             @Override
             public void success(Object o) {
                 myViewInterFace= (ContarInterFace.MyViewInterFace) tt;
                 myViewInterFace.toShow (o);
             }
         });
    }
}

三 Model

public class Model {

    MyCallBack myCallBack;
    Handler mhandler = new Handler (){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage (msg);
            String json = (String) msg.obj;
            Gson gson = new Gson ();
            Product product = gson.fromJson (json, Product.class);
            myCallBack.success (product);

        }
    };

    public void toRequest(MyCallBack myCallBack){
        this.myCallBack =myCallBack;

        OkHttpUtile.getInstance ().doGet ("http://172.17.8.100/ks/product/getCarts?uid=51", new Callback () {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body ().string ();
                Message message = new Message ();
                message.obj=json;
                mhandler.sendMessage (message);
            }
        });
    }

    public interface MyCallBack{
        public void success(Object o);
    }
}

public class OkHttpUtile {

    OkHttpClient okHttpClient;
    public static OkHttpUtile utile;
    private OkHttpUtile(){
        okHttpClient = new OkHttpClient.Builder ().addInterceptor (new Interceptor () {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request ();
                Response response = chain.proceed (request);
                return response;
            }
        }).build ();
    }

    public static synchronized OkHttpUtile getInstance(){
        if(utile == null){
            utile = new OkHttpUtile ();
        }
        return utile;
    }

    public void doGet(String url, Callback callback){
        Request request = new Request.Builder ()
                .url (url)
                .get ()
                .build ();

        Call call = okHttpClient.newCall (request);
        call.enqueue (callback);
    }
}

五 MyAdapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holde> {

    Context context;
    List<Product.DataBean> list;
    MainActivity mainActivity;
    private MyCallBack myCallBack;
    public MyAdapter(Context context, List<Product.DataBean> list) {
        this.context = context;
        this.list = list;
        mainActivity= (MainActivity) context;
    }

    @NonNull
    @Override
    public Holde onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from (context).inflate (R.layout.activity_item, null);
        return new Holde(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final Holde holde, final int i) {
        holde.text1.setText (list.get (i).getSellerName ());
        holde.check1.setChecked (list.get (i).isSelect);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        holde.recyclerView.setLayoutManager(layoutManager);

        PrAdapter prAdapter = new PrAdapter(context,list.get (i).getList ());
        holde.recyclerView.setAdapter (prAdapter);
        holde.check1.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View v) {
                boolean checked = holde.check1.isChecked ();
                list.get (i).isSelect=checked;

                for (int j=0;j<list.get (i).getList ().size ();j++){
                    list.get (i).getList ().get (j).itemSelect=checked;
                }
                myCallBack.setChild (list);
            }
        });
        prAdapter.setMyCallBack (new PrAdapter.MyCallBack () {
            @Override
            public void setChild(List<Product.DataBean.ListBean> lists) {
                int ss=0;
                for (int j=0;j<lists.size ();j++){
                    if (lists.get (j).itemSelect){
                        ss++;
                    }
                }
                if(ss == lists.size ()){
                    holde.check1.setChecked (true);
                    list.get (i).isSelect =true;
                }else{
                    holde.check1.setChecked (false);
                    list.get (i).isSelect=false;
                }
              myCallBack.setChild (list);
            }
        });

    }

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

    public class Holde extends RecyclerView.ViewHolder {
        CheckBox check1;
        TextView text1;
        RecyclerView recyclerView;
        public Holde(@NonNull View itemView) {
            super (itemView);
             check1 = itemView.findViewById (R.id.check1);
             text1 = itemView.findViewById (R.id.text1);
             recyclerView = itemView.findViewById (R.id.recyclerView);
        }
    }
    public void setMyCallBack(MyCallBack myCallBack){
        this.myCallBack = myCallBack;
    }

    public interface MyCallBack{
        public void setChild(List<Product.DataBean> list);
    }
}

其 Product

public class PrAdapter extends RecyclerView.Adapter<PrAdapter.Holde1> {

    Context context;
    List<Product.DataBean.ListBean> list;
    private MyCallBack myCallBack;

    public PrAdapter(Context context, List<Product.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public Holde1 onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from (context).inflate (R.layout.activity_item1, null);
        return new Holde1(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final Holde1 holde1, final int i) {
         holde1.text2.setText (list.get (i).getTitle ());
         Glide.with (context).load (list.get (i).getImages ()).into (holde1.image2);
         holde1.jia.setText (list.get (i).getPrice ()+"");
         holde1.sum.setText(list.get(i).sum+"");
         holde1.check2.setChecked (list.get (i).itemSelect);
         holde1.check2.setOnClickListener (new View.OnClickListener () {
             @Override
             public void onClick(View v) {
                 boolean b = holde1.check2.isChecked ();
                 list.get (i).itemSelect=b;
                 myCallBack.setChild (list);
             }
         });
        holde1.jiajia.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View v) {
                list.get (i).sum++;
                myCallBack.setChild (list);
                holde1.sum.setText (list.get (i).sum+"");
            }
        });
        holde1.jianjian.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View v) {
                if(list.get(i).sum == 1){
                    return ;
                }
                list.get (i).sum--;
                myCallBack.setChild (list);
                holde1.sum.setText (list.get (i).sum+"");
            }
        });
    }

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

    public class Holde1 extends RecyclerView.ViewHolder {
        CheckBox check2;
        TextView text2,jia,jiajia,jianjian,sum;
        ImageView image2;
        public Holde1(@NonNull View itemView) {
            super (itemView);
            check2 = itemView.findViewById (R.id.check2);
            text2 = itemView.findViewById (R.id.pic);
            image2 = itemView.findViewById (R.id.image2);
            jia = itemView.findViewById (R.id.jia);
            jiajia = itemView.findViewById (R.id.jiajia);
            jianjian = itemView.findViewById (R.id.jianjian);
            sum = itemView.findViewById (R.id.text_child_sum);

        }
    }
    public void setMyCallBack(MyCallBack myCallBack){
        this.myCallBack = myCallBack;
    }

    public interface MyCallBack{
        public void setChild(List<Product.DataBean.ListBean> list);
    }
}

把 bean

public boolean itemSelect;
            private String bargainPrice;
            private String createtime;
            private String detailUrl;
            private String images;
            private int num;
            public int sum=1;
            private String pid;
            private int price;
            private String pscid;
            private String selected;
            private String sellerid;
            private String subhead;
            private String title;

public interface ContarInterFace {

public interface PresenterInterFace{
    public void toModel();
}

public interface MyViewInterFace{
    public void toShow(Object o);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值