购物车--ExpandableListView实现

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.bwei.gouwuchemoni.MainActivity">

    <ExpandableListView
        android:id="@+id/elc_show_main"
        android:layout_weight="1"
        android:background="#fff"
        android:layout_width="match_parent"
        android:layout_height="0dp">
    </ExpandableListView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/cb_allCheck_main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选"
            android:textColor="#000" />

        <TextView
            android:id="@+id/btn_allPrice_main"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="价格"
            android:textColor="@android:color/holo_red_dark"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn_allNumber_main"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="去结算(0)" />

    </LinearLayout>

</LinearLayout>

java代码

package com.bwei.gouwuchemoni;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    String url = "https://www.zhaoapi.cn/product/getCarts?uid=71";
    private ExpandableListView elc_show_main;
    private CheckBox cb_allCheck_main;
    private TextView btn_allPrice_main;
    private Button btn_allNumber_main;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        //得到okhttp
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        Request request = new Request.Builder().url(url).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        CartInfo cartInfo = gson.fromJson(string, CartInfo.class);
                        List<CartInfo.DataBean> sellerData = cartInfo.getData();
                        //创建适配
                        myAdapter = new MyAdapter(sellerData);
                        //调用适配器的借口 来实现回电数据
                        myAdapter.setOnCartListChangeListener(new MyAdapter.OnCartListChangeListener() {

                            @Override
                            public void SellerSelectedChange(int groupPosition) {
                                //先得到 checkbox
                                boolean b = myAdapter.isCurrentSellerAllProductSelected(groupPosition);
                                //改变所有当前商家的选中状态
                                myAdapter.changeCurrentSellerAllProductSelected(groupPosition,!b);
                                myAdapter.notifyDataSetChanged();
                                refreshAllSelectedAndTotalPriceAndTotalNumber();

                            }

                            @Override
                            public void changeCurrentProductSelected(int groupPosition, int childPosition) {
                                myAdapter.changeCurrentProductSelected(groupPosition,childPosition);
                                myAdapter.notifyDataSetChanged();
                                refreshAllSelectedAndTotalPriceAndTotalNumber();
                            }

                            @Override
                            public void ProductNumberChange(int groupPosition, int childPosition, int number) {
                                myAdapter.changeCurrentProductNumber(groupPosition,childPosition,number);
                                myAdapter.notifyDataSetChanged();
                                //刷新底部的方法
                                refreshAllSelectedAndTotalPriceAndTotalNumber();
                            }
                        });

                        elc_show_main.setAdapter(myAdapter);
//
                        for (int i = 0; i <sellerData.size() ; i++) {
                            elc_show_main.expandGroup(i);

                        }
                    }
                });
            }
        });

    }

    private void initView() {
        elc_show_main = (ExpandableListView) findViewById(R.id.elc_show_main);
        cb_allCheck_main = (CheckBox) findViewById(R.id.cb_allCheck_main);
        btn_allPrice_main = (TextView) findViewById(R.id.btn_allPrice_main);
        btn_allNumber_main = (Button) findViewById(R.id.btn_allNumber_main);
        cb_allCheck_main.setOnClickListener(this);
        btn_allNumber_main.setOnClickListener(this);
    }
    private void  refreshAllSelectedAndTotalPriceAndTotalNumber(){
        
        boolean allProductsSelected = myAdapter.isAllProductsSelected();
        cb_allCheck_main.setChecked(allProductsSelected);
//计算总金额
        Double totalPrice = myAdapter.calculateTotalPrice();
        btn_allPrice_main.setText("总价:¥"+totalPrice);
        //计算总数量
        int totalNumber = myAdapter.calculateTotalNumber();
        btn_allNumber_main.setText("去结算("+totalNumber+")");
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_allNumber_main:

                break;
            case R.id.cb_allCheck_main:
                boolean allProductsSelected = myAdapter.isAllProductsSelected();
                myAdapter.changeAllProductsSelected(!allProductsSelected);
                myAdapter.notifyDataSetChanged();
                //刷新底部的方法
                refreshAllSelectedAndTotalPriceAndTotalNumber();
                break;

        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值