访问网络更新二级列表

public class ShowActivity extends AppCompatActivity implements ICart, IGeng, View.OnClickListener {


    private ExpandableListView el;
    private ExpandPre expandPre;
    private GengPre gengPre;
    private List<ExpandBean.DataBean> data;
    private CheckBox check_all;
    private TextView sum_price;
    private TextView jiesuan;
    private ProgressBar progressBar;
    private ExpandAdapter adp;
    List<ExpandBean.DataBean.ListBean> childlist = new ArrayList();
    List<ExpandBean.DataBean.ListBean> groupchildlist = new ArrayList<>();
    private boolean isck = false;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                //更新成功重新访问购物车,改变孩子的状态
                expandPre.setPath("https://www.zhaoapi.cn/product/getCarts?uid=2775&token=C1264FBBC844ADB47FFAC4333C20E8EE");
                //改变组的状态
                isChildCheck();
                //改变全选状态
                boolean b = isAllChildCheck();
                check_all.setChecked(b);
                changePriceAndCount();
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        findViewById();
        //查询购物车
        expandPre = new ExpandPre(this);
        gengPre = new GengPre(this);
        //https://www.zhaoapi.cn/product/getCarts?uid=2775&token=C1264FBBC844ADB47FFAC4333C20E8EE
        expandPre.setPath("https://www.zhaoapi.cn/product/getCarts?uid=2775&token=C1264FBBC844ADB47FFAC4333C20E8EE");
        jiesuan.setOnClickListener(this);
        check_all.setOnClickListener(this);
    }


    private void findViewById() {
        progressBar = findViewById(R.id.pb);
        jiesuan = findViewById(R.id.jiesuan);
        el = findViewById(R.id.el);
        check_all = findViewById(R.id.check_all);
        sum_price = findViewById(R.id.sum_price);
    }


    @Override
    public void success(ExpandBean expandBean) {
        progressBar.setVisibility(View.INVISIBLE);
        data = expandBean.getData();
        isChildCheck();
        boolean b = isAllChildCheck();
        check_all.setChecked(b);
        changePriceAndCount();
        adp = new ExpandAdapter(ShowActivity.this, data);
        el.setAdapter(adp);
        for (int i = 0; i < data.size(); i++) {
            el.expandGroup(i);
        }
        adp.setLinstner(new IPosition() {
            @Override
            public void getGroup(int group, Boolean b) {
                count = 0;
                size = 0;
                childlist.clear();
                List<ExpandBean.DataBean.ListBean> list = data.get(group).getList();
                groupchildlist.addAll(list);
                size = childlist.size();
                OkHttp3Util.doGet("https://www.zhaoapi.cn/product/updateCarts?uid=2775&sellerid=" + list.get(count).getSellerid() + "&pid=" + list.get(count).getPid() + "&num=" + list.get(count).getNum() + "&selected=" + String.valueOf(b ? 1 : 0) + "&token=C1264FBBC844ADB47FFAC4333C20E8EE", new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {


                    }


                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful()) {
                            final String string = response.body().string();
                            CommonUtils.runOnUIThread(new Runnable() {
                                @Override
                                public void run() {
                                    AddBean addBean = new Gson().fromJson(string, AddBean.class);
                                    if ("success".equals(addBean.getMsg())) {
                                        expandPre.setPath("https://www.zhaoapi.cn/product/getCarts?uid=2775&token=C1264FBBC844ADB47FFAC4333C20E8EE");
                                    }
                                }
                            });
                        }
                    }
                });
                progressBar.setVisibility(View.VISIBLE);
            }


            @Override
            public void getChild(int group, int child, Boolean b) {
                List<ExpandBean.DataBean.ListBean> list = data.get(group).getList();
                OkHttp3Util.doGet("https://www.zhaoapi.cn/product/updateCarts?uid=2775&sellerid=" + list.get(child).getSellerid() + "&pid=" + list.get(child).getPid() + "&num=" + list.get(child).getNum() + "&selected=" + String.valueOf(b ? 1 : 0) + "&token=C1264FBBC844ADB47FFAC4333C20E8EE", new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {


                    }


                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful()) {
                            final String string = response.body().string();
                            CommonUtils.runOnUIThread(new Runnable() {
                                @Override
                                public void run() {
                                    AddBean addBean = new Gson().fromJson(string, AddBean.class);
                                    if ("success".equals(addBean.getMsg())) {
                                        expandPre.setPath("https://www.zhaoapi.cn/product/getCarts?uid=2775&token=C1264FBBC844ADB47FFAC4333C20E8EE");
                                    }
                                }
                            });
                        }
                    }
                });
                progressBar.setVisibility(View.VISIBLE);
            }


            @Override
            public void getJia(int group, int child, Boolean b) {
                List<ExpandBean.DataBean.ListBean> list = data.get(group).getList();
                OkHttp3Util.doGet("https://www.zhaoapi.cn/product/updateCarts?uid=2775&sellerid=" + list.get(child).getSellerid() + "&pid=" + list.get(child).getPid() + "&num=" + (list.get(child).getNum() + 1) + "&selected=" + String.valueOf(b ? 1 : 0) + "&token=C1264FBBC844ADB47FFAC4333C20E8EE", new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {


                    }


                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful()) {
                            final String string = response.body().string();
                            CommonUtils.runOnUIThread(new Runnable() {
                                @Override
                                public void run() {
                                    AddBean addBean = new Gson().fromJson(string, AddBean.class);
                                    if ("success".equals(addBean.getMsg())) {
                                        expandPre.setPath("https://www.zhaoapi.cn/product/getCarts?uid=2775&token=C1264FBBC844ADB47FFAC4333C20E8EE");
                                    }
                                }
                            });
                        }
                    }
                });
                progressBar.setVisibility(View.VISIBLE);
            }


            @Override
            public void getJian(int group, int child, Boolean b) {
                List<ExpandBean.DataBean.ListBean> list = data.get(group).getList();
                OkHttp3Util.doGet("https://www.zhaoapi.cn/product/updateCarts?uid=2775&sellerid=" + list.get(child).getSellerid() + "&pid=" + list.get(child).getPid() + "&num=" + (list.get(child).getNum() - 1) + "&selected=" + String.valueOf(b ? 1 : 0) + "&token=C1264FBBC844ADB47FFAC4333C20E8EE", new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {


                    }


                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful()) {
                            final String string = response.body().string();
                            CommonUtils.runOnUIThread(new Runnable() {
                                @Override
                                public void run() {
                                    AddBean addBean = new Gson().fromJson(string, AddBean.class);
                                    if ("success".equals(addBean.getMsg())) {
                                        expandPre.setPath("https://www.zhaoapi.cn/product/getCarts?uid=2775&token=C1264FBBC844ADB47FFAC4333C20E8EE");
                                    }
                                }
                            });
                        }
                    }
                });
                progressBar.setVisibility(View.VISIBLE);
            }
        });


    }


    private void changePriceAndCount() {
        int sum = 0;
        int count = 0;
        for (int i = 0; i < data.size(); i++) {
            List<ExpandBean.DataBean.ListBean> list = data.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getSelected() == 1) {
                    sum += list.get(j).getBargainPrice() * list.get(j).getNum();
                    count += list.get(j).getNum();
                }
            }
            sum_price.setText(sum + "");
            jiesuan.setText("去结算" + count);
        }
    }


    private int count = 0;
    private int size = 0;


    @Override
    public void geng(String msg) {
        count++;
        if (("success").equals(msg)) {
            if (count == size) {
                Message handlermsg = Message.obtain();
                handlermsg.what = 0;
                handler.sendMessage(handlermsg);
            } else {
                gengPre.setPath("https://www.zhaoapi.cn/product/updateCarts?uid=2775&sellerid=" + childlist.get(count).getSellerid() + "&pid=" + childlist.get(count).getPid() + "&num=" + childlist.get(count).getNum() + "&selected=" + String.valueOf(isck ? 1 : 0) + "&token=C1264FBBC844ADB47FFAC4333C20E8EE");
            }


        }




    }


    private boolean isAllChildCheck() {
        for (int i = 0; i < data.size(); i++) {
            List<ExpandBean.DataBean.ListBean> list = data.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getSelected() == 0) {
                    return false;
                }
            }
        }
        return true;
    }


    private void isChildCheck() {
        for (int i = 0; i < data.size(); i++) {
            List<ExpandBean.DataBean.ListBean> list = data.get(i).getList();
            if (childCheckAll(list)) {
                data.get(i).setGroupCheck(true);
            }
        }
    }


    private boolean childCheckAll(List<ExpandBean.DataBean.ListBean> list) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSelected() == 0) {
                return false;
            }
        }
        return true;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.jiesuan:
                Intent intent = new Intent(ShowActivity.this, DingDanAvtivity.class);
                String sprice = sum_price.getText().toString();
                Log.i("总价---",sprice);
                intent.putExtra("sum", sprice);
                startActivity(intent);
                break;
            case R.id.check_all:
                isck = check_all.isChecked();
                changeAllState(check_all.isChecked());
                break;
        }
    }


    private void changeAllState(boolean checked) {
        count = 0;
        size = 0;
        childlist.clear();
        for (int i = 0; i < data.size(); i++) {
            childlist.addAll(data.get(i).getList());
        }
        size = childlist.size();
        gengPre.setPath("https://www.zhaoapi.cn/product/updateCarts?uid=2775&sellerid=" + childlist.get(count).getSellerid() + "&pid=" + childlist.get(count).getPid() + "&num=" + childlist.get(count).getNum() + "&selected=" + String.valueOf(checked ? 1 : 0) + "&token=C1264FBBC844ADB47FFAC4333C20E8EE");
        progressBar.setVisibility(View.VISIBLE);
    }

}



public class ExpandAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<ExpandBean.DataBean> data;
    private IPosition iPosition;


    public ExpandAdapter(Context context, List<ExpandBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }


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


    @Override
    public int getChildrenCount(int i) {
        return data.get(i).getList().size();
    }


    @Override
    public Object getGroup(int i) {
        return data.get(i);
    }


    @Override
    public Object getChild(int i, int i1) {
        return data.get(i).getList().get(i1);
    }


    @Override
    public long getGroupId(int i) {
        return i;
    }


    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }


    @Override
    public boolean hasStableIds() {
        return true;
    }


    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        ExpandBean.DataBean dataBean = data.get(i);
        GroupHolder groupHolder = null;
        if (view == null) {
            view = View.inflate(context, R.layout.group_item_layout, null);
            groupHolder = new GroupHolder();
            groupHolder.checkBox = view.findViewById(R.id.check_group);
            groupHolder.textView = view.findViewById(R.id.text_group);
            view.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) view.getTag();
        }
        groupHolder.checkBox.setChecked(dataBean.isGroupCheck());
        groupHolder.textView.setText(dataBean.getSellerName());


        final GroupHolder finalGroupHolder = groupHolder;
        groupHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iPosition.getGroup(i, finalGroupHolder.checkBox.isChecked());
            }
        });
        return view;
    }


    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        final ChildHolder childHolder;
        if (view == null) {
            view = View.inflate(context, R.layout.child_item_layout, null);
            childHolder = new ChildHolder();
            childHolder.check_child = view.findViewById(R.id.check_child);
            childHolder.text_price = view.findViewById(R.id.text_price);
            childHolder.text_jian = view.findViewById(R.id.text_jian);
            childHolder.text_add = view.findViewById(R.id.text_add);
            childHolder.text_num = view.findViewById(R.id.text_num);
            childHolder.text_title = view.findViewById(R.id.text_title);
            childHolder.image_good = view.findViewById(R.id.image_good);
            view.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) view.getTag();
        }
        ExpandBean.DataBean.ListBean listBean = data.get(i).getList().get(i1);
        childHolder.check_child.setChecked(listBean.getSelected() == 1 ? true : false);
        childHolder.text_price.setText(listBean.getPrice() + "");
        childHolder.text_num.setText(listBean.getNum() + "");
        childHolder.text_title.setText(listBean.getTitle());
        Glide.with(context).load(listBean.getImages().split("\\|")[0]).into(childHolder.image_good);
        childHolder.check_child.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iPosition.getChild(i, i1,childHolder.check_child.isChecked());
            }
        });
        childHolder.text_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iPosition.getJia(i, i1,childHolder.check_child.isChecked());
            }
        });
        childHolder.text_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iPosition.getJian(i, i1,childHolder.check_child.isChecked());
            }
        });
        return view;
    }


    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }


    public void setLinstner(IPosition iPosition) {
        this.iPosition = iPosition;
    }


    class GroupHolder {
        CheckBox checkBox;
        TextView textView;
    }


    class ChildHolder {
        CheckBox check_child;
        ImageView image_good;
        TextView text_title;
        TextView text_price;
        TextView text_jian;
        TextView text_num;
        TextView text_add;
    }
}


public interface ICart {
    void success(ExpandBean expandBean);
}

public interface IGeng {
    void geng(String msg);
}


public interface IPosition {
    void getGroup(int group,Boolean b);
    void getChild(int group,int child,Boolean b);
    void getJia(int group,int child,Boolean b);
    void getJian(int group,int child,Boolean b);
}


group布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_gravity="center_vertical"
    android:layout_width="match_parent"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:layout_height="40dp">


    <CheckBox
        android:id="@+id/check_group"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:background="@drawable/check_box_selector"
        android:button="@null" />


    <TextView
        android:id="@+id/text_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:text=">"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

     child布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingTop="10dp"
    android:divider="@null"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <CheckBox
            android:id="@+id/check_child"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_gravity="center_vertical"
            android:background="@drawable/check_box_selector"
            android:button="@null" />


        <ImageView
            android:layout_marginLeft="10dp"
            android:id="@+id/image_good"
            android:layout_width="220px"
            android:layout_height="220px"
            android:background="@mipmap/ic_launcher" />


        <LinearLayout
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <TextView
                android:id="@+id/text_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="qqq" />


            <LinearLayout
                android:gravity="bottom"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


                <TextView
                    android:id="@+id/text_price"
                    android:textColor="#f00"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="qqq" />


                <TextView
                    android:background="@drawable/border"
                    android:layout_marginLeft="60dp"
                    android:id="@+id/text_jian"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:text="-" />


                <TextView
                    android:background="@drawable/border"
                    android:id="@+id/text_num"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_marginLeft="10dp"
                    android:text="qqq" />


                <TextView
                    android:background="@drawable/border"
                    android:id="@+id/text_add"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_marginLeft="10dp"
                    android:text="+" />
            </LinearLayout>




        </LinearLayout>
        <Button
            android:layout_gravity="center"
            android:background="#f00"
            android:text="删除"
            android:layout_width="80dp"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>


      购物车布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.mounth1219.view.view.ShowActivity">
      <ExpandableListView
          android:id="@+id/el"
          android:layout_width="match_parent"
          android:layout_height="match_parent">


      </ExpandableListView>
      <LinearLayout
          android:layout_alignParentBottom="true"
          android:layout_width="match_parent"
          android:layout_height="50dp">
            <CheckBox
                android:button="@null"
                android:background="@drawable/check_box_selector"
                android:id="@+id/check_all"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:text="¥:"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/sum_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <Button
                android:background="#f00"
                android:id="@+id/jiesuan"
                android:layout_marginLeft="250dp"
                android:text="去结算"
                android:layout_width="80dp"
                android:layout_height="wrap_content" />
      </LinearLayout>
      <ProgressBar
          android:visibility="invisible"
          android:layout_centerInParent="true"
          android:id="@+id/pb"
          android:layout_width="50dp"
          android:layout_height="50dp" />
</RelativeLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值