一级列表的购物车

来源:互联网 发布:淘宝延迟收货在哪里 编辑:程序博客网 时间:2024/04/16 20:10

//先把布局加载出来

MainActivity

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="test.bwei.com.gouwuche666.MainActivity">    <android.support.v7.widget.RecyclerView        android:id="@+id/r1_review"        android:layout_above="@+id/b"        android:layout_width="match_parent"        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>    <RelativeLayout        android:id="@+id/b"        android:padding="10dp"        android:layout_alignParentBottom="true"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <CheckBox            android:id="@+id/selectall"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/j"            android:layout_width="wrap_content"            android:padding="10dp"            android:text="价钱:¥"            android:textColor="#000"            android:layout_toRightOf="@+id/selectall"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/sumnoney"            android:layout_width="wrap_content"            android:padding="6dp"            android:text="money"            android:textSize="17dp"            android:textColor="#c00"            android:layout_toRightOf="@+id/j"            android:layout_height="wrap_content" />    </RelativeLayout></RelativeLayout>

//childitem

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <CheckBox    android:id="@+id/good_cb"    android:layout_centerVertical="true"    android:layout_width="wrap_content"    android:layout_height="wrap_content" />    <ImageView        android:id="@+id/good_iv"        android:layout_toRightOf="@+id/good_cb"        android:src="@mipmap/ic_launcher"        android:layout_width="80dp"        android:layout_height="80dp" />    <TextView        android:id="@+id/good_shop"        android:padding="6dp"        android:layout_toRightOf="@+id/good_iv"        android:text="店名"        android:textSize="15dp"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/buy_content"        android:text="context"        android:layout_below="@+id/good_shop"        android:layout_toRightOf="@+id/good_iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:textColor="#c00"        android:id="@+id/good_money"        android:text="context"        android:layout_below="@+id/buy_content"        android:layout_toRightOf="@+id/good_iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/good_j"        android:text="X "        android:layout_marginLeft="80dp"        android:layout_below="@+id/buy_content"        android:layout_toRightOf="@+id/good_money"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/good_num"        android:text="mone "        android:layout_below="@+id/buy_content"        android:layout_toRightOf="@+id/good_j"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></RelativeLayout>

//item

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <CheckBox        android:id="@+id/boss_all"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:background="#ccc"        android:layout_toRightOf="@+id/boss_all"        android:id="@+id/boss"        android:padding="10dp"        android:text="boss"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <android.support.v7.widget.RecyclerView        android:layout_below="@+id/boss"        android:id="@+id/re2"        android:layout_width="match_parent"        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></RelativeLayout>

//请求的APi地址

public class Api {    public static String UPDATA_URL="http://120.27.23.105/product/updateCarts?uid=144&sellerid=1&pid=1&selected=0&num=10";    public static String LOOK_URL="http://120.27.23.105/product/getCarts?uid=144";    public static String DELETE="http://120.27.23.105/product/deleteCart?uid=144";}

//MainActivity

public class MainActivity extends AppCompatActivity {    private RecyclerView recycle;    private CheckBox cball;    private TextView money;    private List<Bean.DataBean> data;    private MyAdapter ada;    private int kb;    private double money1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initview();        initdata();        setlistener();    }    private void setlistener() {        cball.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                if(cball.isChecked()){                    for (Bean.DataBean bean : data) {                        bean.setFlag(true);                        List<Bean.DataBean.ListBean> list = bean.getList();                        for (Bean.DataBean.ListBean listBean : list) {                            listBean.setSelected(1);                        }                    }                }else{                    for (Bean.DataBean bean : data) {                        bean.setFlag(false);                        List<Bean.DataBean.ListBean> list = bean.getList();                        for (Bean.DataBean.ListBean listBean : list) {                            listBean.setSelected(0);                        }                    }                }                ada.notifyDataSetChanged();            }        });    }    private void initdata() {        NetUtils.getResult(Api.LOOK_URL, new NetUtils.CallResult() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) {                try {                    String str=response.body().string();                    Gson gson=new Gson();                    Bean bean = gson.fromJson(str, Bean.class);                    data = bean.getData();                    runOnUiThread(new Runnable() {                        @Override                        public void run() {                            ada= new MyAdapter((ArrayList<Bean.DataBean>) data,MainActivity.this);                            recycle.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));                            recycle.setAdapter(ada);                                                        ada.setMoneyResult(new MyAdapter.MoneyResult() {                                @Override                                public void money() {                                    kb= 0;                                    money1= 0;                                    for (Bean.DataBean dataBean : data) {                                        List<Bean.DataBean.ListBean> been = dataBean.getList();                                        for (Bean.DataBean.ListBean listBean : been) {                                            if(listBean.getSelected()==1){                                                kb++;                                                money1+=listBean.getBargainPrice()*listBean.getNum();                                            }                                        }                                    }                                    //设置总价                                    money.setText(money1+"");                                }                            });                        }                    });                } catch (IOException e) {                    e.printStackTrace();                }            }        });    }    private void initview() {        recycle = (RecyclerView) findViewById(R.id.r1_review);        cball = (CheckBox) findViewById(R.id.selectall);        money = (TextView) findViewById(R.id.sumnoney);    }

//Bean

public class Bean {    private String msg;    private String code;    private List<DataBean> data;    public String getMsg() {        return msg;    }    public void setMsg(String msg) {        this.msg = msg;    }    public String getCode() {        return code;    }    public void setCode(String code) {        this.code = code;    }    public List<DataBean> getData() {        return data;    }    public void setData(List<DataBean> data) {        this.data = data;    }    public static class DataBean {               private String sellerName;        private String sellerid;        private List<ListBean> list;        private boolean flag = false;        public boolean isFlag() {            return flag;        }        public void setFlag(boolean flag) {            this.flag = flag;        }        public String getSellerName() {            return sellerName;        }        public void setSellerName(String sellerName) {            this.sellerName = sellerName;        }        public String getSellerid() {            return sellerid;        }        public void setSellerid(String sellerid) {            this.sellerid = sellerid;        }        public List<ListBean> getList() {            return list;        }        public void setList(List<ListBean> list) {            this.list = list;        }        public static class ListBean {                       private double bargainPrice;            private String createtime;            private String detailUrl;            private String images;            private int num;            private int pid;            private double price;            private int pscid;            private int selected;            private int sellerid;            private String subhead;            private String title;            public double getBargainPrice() {                return bargainPrice;            }            public void setBargainPrice(double bargainPrice) {                this.bargainPrice = bargainPrice;            }            public String getCreatetime() {                return createtime;            }            public void setCreatetime(String createtime) {                this.createtime = createtime;            }            public String getDetailUrl() {                return detailUrl;            }            public void setDetailUrl(String detailUrl) {                this.detailUrl = detailUrl;            }            public String getImages() {                return images;            }            public void setImages(String images) {                this.images = images;            }            public int getNum() {                return num;            }            public void setNum(int num) {                this.num = num;            }            public int getPid() {                return pid;            }            public void setPid(int pid) {                this.pid = pid;            }            public double getPrice() {                return price;            }            public void setPrice(double price) {                this.price = price;            }            public int getPscid() {                return pscid;            }            public void setPscid(int pscid) {                this.pscid = pscid;            }            public int getSelected() {                return selected;            }            public void setSelected(int selected) {                this.selected = selected;            }            public int getSellerid() {                return sellerid;            }            public void setSellerid(int sellerid) {                this.sellerid = sellerid;            }            public String getSubhead() {                return subhead;            }            public void setSubhead(String subhead) {                this.subhead = subhead;            }            public String getTitle() {                return title;            }            public void setTitle(String title) {                this.title = title;            }        }    }

//utilds   NetUtils

public class NetUtils {    public static void getResult(String url, final CallResult callResult){//        OkHttpClient client = SingleOk.getInstence()//                .connectTimeout(10, TimeUnit.SECONDS)//                .readTimeout(10, TimeUnit.SECONDS)//                .writeTimeout(10, TimeUnit.SECONDS)//                .retryOnConnectionFailure(false)//                .build();        OkHttpClient client =new OkHttpClient.Builder().build();        FormBody.Builder build=new FormBody.Builder();        RequestBody body=build.build();        Request request=new Request.Builder().post(body).url(url).build();        client.newCall(request).enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {                if(callResult!=null){                    callResult.onFailure(call,e);                }            }            @Override            public void onResponse(Call call, Response response) throws IOException {                if(callResult!=null) {                    callResult.onResponse(call, response);                }            }        });    }    public interface CallResult{        void onFailure(Call call, IOException e);      void onResponse(Call call, Response response);    }
//适配器

//大的适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {    private ArrayList<Bean.DataBean> list;    private Context context;    private ChildAdapter ada;    public MyAdapter(ArrayList<Bean.DataBean> list, Context context) {        this.list = list;        this.context = context;    }    @Override    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view=View.inflate(context, R.layout.item,null);        return new MyHolder(view);    }    @Override    public void onBindViewHolder(final MyHolder holder, final int position) {        holder.title.setText(list.get(position).getSellerName());        final List<Bean.DataBean.ListBean> l = this.list.get(position).getList();        ada = new ChildAdapter((ArrayList<Bean.DataBean.ListBean>) l,context);        holder.lv.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));        holder.lv.setAdapter(ada);        //根据数据,状态改变        holder.cb.setChecked(list.get(position).isFlag());        //商家改变子商品        holder.cb.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                list.get(position).setFlag(!list.get(position).isFlag());                for (int i = 0; i < l.size(); i++) {                    if (list.get(position).isFlag()){                        l.get(i).setSelected(1);                    }else {                        l.get(i).setSelected(0);                    }                }                holder.lv.setAdapter(ada);                notifyDataSetChanged();              moneyResult.money();            }        });        //计算钱        ada.setCallMoney(new ChildAdapter.CallMoney() {            @Override            public void changeMoney() {                moneyResult.money();            }        });        //子改变商家cb        ada.setOnItemAllClick(new ChildAdapter.OnItemAllClick() {            @Override            public void all() {                list.get(position).setFlag(true);                holder.cb.setChecked(true);                notifyDataSetChanged();            }            @Override            public void noall() {                list.get(position).setFlag(false);                holder.cb.setChecked(false);                notifyDataSetChanged();            }        });    }    @Override    public int getItemCount() {        return list.size();    }    class MyHolder extends RecyclerView.ViewHolder{        private RecyclerView lv;        private TextView title;        private CheckBox cb;        public MyHolder(View itemView) {            super(itemView);            lv=itemView.findViewById(R.id.re2);            title=itemView.findViewById(R.id.boss);            cb=itemView.findViewById(R.id.boss_all);        }    }    private MoneyResult moneyResult;    public void setMoneyResult(MoneyResult moneyResult) {        this.moneyResult = moneyResult;    }    public interface MoneyResult{        void money();    }}

//小的适配器ChildAdapter

public class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.MHolder> {    private ArrayList<Bean.DataBean.ListBean> list;    private Context context;    public ChildAdapter(ArrayList<Bean.DataBean.ListBean> list, Context context) {        this.list = list;        this.context = context;    }    @Override    public MHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view=View.inflate(context, R.layout.childitem,null);        return new MHolder(view);    }    @Override    public void onBindViewHolder(final MHolder holder, final int position) {        holder.title.setText(list.get(position).getSubhead());        holder.title.setMaxLines(1);        holder.money.setText(list.get(position).getBargainPrice()+"");        holder.c.setText(list.get(position).getTitle());        holder.c.setMaxLines(1);        holder.num.setText(list.get(position).getNum()+"");        String[] split = list.get(position).getImages().split("\\|");        Glide.with(context).load(split[0]).into(holder.iv);        //根据数据改变状态        if(list.get(position).getSelected()==1){            holder.cb.setChecked(true);        }else{            holder.cb.setChecked(false);        }        //子商品的点击事件        holder.cb.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                if (holder.cb.isChecked()){                    holder.cb.setChecked(true);                    list.get(position).setSelected(1);                }else {                    holder.cb.setChecked(false);                    list.get(position).setSelected(0);                }                all();                notifyDataSetChanged();                callMoney.changeMoney();            }        });    }    //判断是否是全选的回调    private int sum=0;    private void all() {        for (int i = 0; i <list.size() ; i++) {            if(list.get(i).getSelected()==1){                sum++;            }else{                sum--;            }        }        if(sum==list.size()){            onItemAllClick.all();        }else{            onItemAllClick.noall();        }        sum=0;    }    private OnItemAllClick onItemAllClick;    public void setOnItemAllClick(OnItemAllClick onItemAllClick) {        this.onItemAllClick = onItemAllClick;    }    public interface OnItemAllClick{        void all();        void noall();    }    @Override    public int getItemCount() {        return list.size();    }    class MHolder extends RecyclerView.ViewHolder{        ImageView iv;        TextView money;        TextView title;        TextView c;        CheckBox cb;        TextView num;        public MHolder(View itemView) {            super(itemView);            iv=itemView.findViewById(R.id.good_iv);            money=itemView.findViewById(R.id.good_money);            title=itemView.findViewById(R.id.good_shop);            c=itemView.findViewById(R.id.buy_content);            cb=itemView.findViewById(R.id.good_cb);            num=itemView.findViewById(R.id.good_num);        }    }    private CallMoney callMoney;    public void setCallMoney(CallMoney callMoney) {        this.callMoney = callMoney;    }    public interface CallMoney{        void changeMoney();    }}


原创粉丝点击