購物車^_^

一、導入依賴

compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.squareup.okhttp3:okhttp:3.3.0'
compile files('libs/gson-2.2.4.jar')

二、添加權限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

三、MainActivity的佈局

<?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:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bw.lenovo.goodscart.MainActivity">

    <TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="購物車"
        android:textSize="25sp"/>

    <View
        android:background="#999999"
        android:layout_width="match_parent"
        android:layout_height="0.75dp"/>

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/expan_list_view"
        android:groupIndicator="@null"></ExpandableListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <CheckBox
            style="@style/Widget.AppCompat.CompoundButton.RadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/xuan"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全選"/>

        <TextView
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/sum"
            android:text="合計:¥0"
            android:textSize="25sp"/>
        <Button
            android:id="@+id/toJisuan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#f00"
            android:text="去結算"/>

    </LinearLayout>
</LinearLayout>

四、自定義控件(加減按鈕)的佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="30dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/sub"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="-"
        android:background="@drawable/add_sub_shape"
        android:gravity="center"
        android:textSize="25sp"
        />

    <EditText
        android:id="@+id/count"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/add_sub_shape"
        android:gravity="center"
        android:text="0"
        android:focusable="false"
        />

    <TextView
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/add_sub_shape"
        android:gravity="center"
        android:text="+"
        android:textSize="25sp"/>
</LinearLayout>

五、ExpandableListView的父視圖佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <CheckBox
        style="@style/Widget.AppCompat.CompoundButton.RadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/parent_cb"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="商家"
        android:textSize="20sp"
        android:id="@+id/parent_title"/>
</LinearLayout>

六、ExpandableListView的子視圖佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <CheckBox
        style="@style/Widget.AppCompat.CompoundButton.RadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/child_cb"/>

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/child_icon"
        android:src="@mipmap/ic_launcher"/>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="商品名稱"
            android:layout_weight="1"
            android:id="@+id/child_title"
            android:textSize="20sp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/child_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="價格:0"
                android:textColor="@android:color/holo_red_light"/>

            <com.bw.lenovo.goodscart.view.AddSubView
                android:id="@+id/add_sub_view"
                android:layout_width="100dp"
                android:layout_height="25dp"
                android:layout_marginLeft="40dp"></com.bw.lenovo.goodscart.view.AddSubView>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

七、http包下的 httpConfig (接口路徑)

package com.bw.lenovo.goodscart.http;

public class HttpConfig {

    public static String url = "https://www.zhaoapi.cn/product/getCarts?source=android&uid=71";
}

八、http包下的 OkhttpUtils(ok封裝網絡框架)

package com.bw.lenovo.lianxi_goodscart.Http;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.IOException;

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

public class OKHttpUtils {

    private static final String TAG = "OKHttpUtils===========";
    private static OKHttpUtils okHttpUtils = null;
    private MyHandler myhandler = new MyHandler();
    private OKLoadLinsener okLoadLinsener;

    public static OKHttpUtils getInstance() {
        if (okHttpUtils == null) {
            okHttpUtils = new OKHttpUtils();
        }
        return okHttpUtils;
    }

    public void okGet(String url) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure() 失敗=====");
                Message message = myhandler.obtainMessage();
                message.what = 0;
                message.obj = e.getMessage();
                myhandler.sendMessage(message);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d(TAG, "onResponse() 成功=====");
                Message message = myhandler.obtainMessage();
                message.what = 1;
                message.obj = response.body().string();
                myhandler.sendMessage(message);
            }
        });
    }

    //將輸入發送到主線程,處理線程問題
    public class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //獲取消息的識別碼  返回int類型的值
            int wh = msg.what;
            switch (wh) {
                //失敗
                case 0:
                    String error = (String) msg.obj;
                    okLoadLinsener.LoadError(error);
                    break;
                //成功
                case 1:
                    String json = (String) msg.obj;
                    okLoadLinsener.LoadSuccess(json);
                    break;

            }
        }
    }

    //創建ok加載時的監聽   (一般加載時就兩種狀態)失敗 +成功
    public interface OKLoadLinsener {
        //成功
        void LoadSuccess(String json);
        //失敗
        void LoadError(String error);
    }

    //    設置提供一個供外部訪問的方法
    public void setOnLoadListener(OKLoadLinsener okLoadLinsener) {
        this.okLoadLinsener = okLoadLinsener;
    }
}

九、http包下的GetCartListLinsener


package com.bw.lenovo.goodscart.http;

import com.bw.lenovo.goodscart.model.CartListBean;
import java.util.List;

/**
 * 獲取購物車的監聽
 */
public interface GetCartListListener {
    void getCartSuccess(List<CartListBean.DataBean> list);
//    失敗
    void getCartError(String error);
}

十、model包下需要寫一個Bean類==解析接口數據(一鍵解析)


注:這個購物車接口有一個爲空(需要手動添加一個childBean)把?問號替換成childBean害羞




十一、model包下寫ICartModel

package com.bw.lenovo.goodscart.model;

import com.bw.lenovo.goodscart.http.GetCartListListener;
/**
 * 去請求購物車數據
 */
public interface ICartModel {
    void getCartList(String url, GetCartListListener getCartListListener);

}


十二、model包下寫 CartModelmpI

package com.bw.lenovo.goodscart.model;

import android.util.Log;

import com.bw.lenovo.goodscart.http.GetCartListListener;
import com.bw.lenovo.goodscart.http.HttpConfig;
import com.bw.lenovo.goodscart.http.OKHttpUtils;
import com.google.gson.Gson;

import java.util.List;

import static android.content.ContentValues.TAG;

public class CartModelImpl implements ICartModel {

    private static final String TAG = "CartModelImpl======";

    @Override
    public void getCartList(String url, final GetCartListListener getCartListListener) {
        OKHttpUtils okHttpUtils = OKHttpUtils.getInstance();
        okHttpUtils.okGet(url);
        okHttpUtils.setOnLoadListener(new OKHttpUtils.OKLoadListener() {
            @Override
            public void loadSuccess(String json) {
                Log.d(TAG, "成功" + json);
                //網絡請求成功
                Gson gson = new Gson();
                CartListBean cartListBean = gson.fromJson(json, CartListBean.class);
                List<CartListBean.DataBean> list = cartListBean.getData();
//                對象
                getCartListListener.getCartSuccess(list);
            }

            @Override
            public void loadError(String error) {
                //網絡請求失敗
                getCartListListener.getCartError(error);
            }
        });
    }
}

十三、view包下的IView

package com.bw.lenovo.goodscart.view;

import com.bw.lenovo.goodscart.model.CartListBean;
import java.util.List;

public interface IView {
    //展示
    void showCartList(List<CartListBean.DataBean> list);

    //顯示總價
    void showCount(String count);
}

十四、IView包下的自定義控件(加減控件)

package com.bw.lenovo.goodscart.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bw.lenovo.goodscart.R;

public class AddSubView extends LinearLayout implements View.OnClickListener {

    private static final String TAG = "AddSubView=============";
    private TextView sub;
    private EditText count;
    private TextView add;
    private AddClickListener addClickListener;
    private SubClickListener subClickListener;

    public AddSubView(Context context) {
        this(context, null);
    }

    public AddSubView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = View.inflate(context, R.layout.add_sub_layout, this);
        //獲取控件
        sub = view.findViewById(R.id.sub);
        count = view.findViewById(R.id.count);
        add = view.findViewById(R.id.add);
        sub.setOnClickListener(this);
        add.setOnClickListener(this);
    }

    //點擊事件
    @Override
    public void onClick(View view) {

        switch (view.getId()) {
              // 當id爲減時
            case R.id.sub:
                   //  獲取當時輸入的內容
                String c = count.getText().toString();
                     // 裝換成int類型
                int i = Integer.parseInt(c);
                   // 判斷i小於0的時候
                if (i <= 0) {
                    //  停止
                    return;
                }

                count.setText(--i + "");
                subClickListener.onSubClick(view, i);
                Log.d(TAG, "減=======" + i);

                break;
            case R.id.add:
                String c1 = count.getText().toString();
                int i1 = Integer.parseInt(c1);

                count.setText(++i1 + "");
                addClickListener.onAddClick(view, i1);
                Log.d(TAG, "onClick() returned:" + "加=======");
                break;
        }
    }

    //獲取內容的方法
    public int getCount() {
        //返回輸入的內容轉換成int類型的去掉空格
        return Integer.parseInt(count.getText().toString().trim());
    }

    public void setCount(int s) {
        count.setText(s + "");
    }


    //定義一個可被外部調用的接口(加)
    public interface AddClickListener {
        void onAddClick(View view, int count);
    }

    // 定義一個可被外部調用的接口(減)
    public interface SubClickListener {
        void onSubClick(View view, int count);
    }

    //加的點擊事件
    public void setOnAddClickListener(AddClickListener addClickListener) {
        this.addClickListener = addClickListener;
    }

    // 減的點擊事件
    public void setOnSubClickListener(SubClickListener subClickListener) {
        this.subClickListener = subClickListener;
    }

}

十五、presenter包下的IPresenter

package com.bw.lenovo.goodscart.presenter;

import com.bw.lenovo.goodscart.model.ICartModel;
import com.bw.lenovo.goodscart.view.IView;

public interface IPresenter {
//    展示數據
    void showCartList(ICartModel iCartModel, IView iView);
}

十六、presenter包下的presenterImpI


package com.bw.lenovo.goodscart.presenter;

import android.net.IpPrefix;

import com.bw.lenovo.goodscart.http.GetCartListListener;
import com.bw.lenovo.goodscart.http.HttpConfig;
import com.bw.lenovo.goodscart.model.CartListBean;
import com.bw.lenovo.goodscart.model.ICartModel;
import com.bw.lenovo.goodscart.view.IView;

import java.util.List;

public class PresenterImpl implements IPresenter{
    @Override
    public void showCartList(ICartModel iCartModel, final IView iView) {
        // 具體的操作
        iCartModel.getCartList(HttpConfig.url, new GetCartListListener() {
            @Override
            public void getCartSuccess(List<CartListBean.DataBean> list) {
           //   傳給view
                iView.showCartList(list);
            }

            @Override
            public void getCartError(String error) {

            }
        });
    }

}

十七、view包下的  ExpandableListView的適配器 


package com.bw.lenovo.goodscart.view;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.NoAnimation;
import com.bw.lenovo.goodscart.R;
import com.bw.lenovo.goodscart.Utils.SumUtils;
import com.bw.lenovo.goodscart.model.CartListBean;
import com.bw.lenovo.goodscart.presenter.PresenterImpl;

import java.util.List;

public class MyExpanableAdapter extends BaseExpandableListAdapter {
    private final Context context;
    private static final String TAG = "MyExpanableAdapter----";
    private final List<CartListBean.DataBean> list;
    private final IView iView;

    public MyExpanableAdapter(Context context, List<CartListBean.DataBean> list, IView iView) {
        this.context = context;
        this.list = list;
        this.iView = iView;
    }

    //    獲取父分組的數量
    @Override
    public int getGroupCount() {
        return list.size();
    }

    //   獲取子分組的數量
    @Override
    public int getChildrenCount(int i) {
        return list.get(i).getList().size();
    }

    //獲取父分組的對象
    @Override
    public Object getGroup(int i) {
        return list.get(i);
    }

    //獲取子分組的對象
    @Override
    public Object getChild(int i, int i1) {
        return list.get(i).getList().get(i1);
    }

    //獲取父分組的id
    @Override
    public long getGroupId(int i) {
        return i;
    }

    //獲取子分組的id
    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    //保持平衡的意思
    @Override
    public boolean hasStableIds() {
        return false;
    }

    //  獲取父分組視圖
    @Override
    public View getGroupView(final int i, boolean b, View convertView, ViewGroup viewGroup) {
        final ParentViewHolder parentViewHolder;

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.parent_layout, null);
            CheckBox parent_cb = convertView.findViewById(R.id.parent_cb);
            TextView parent_title = convertView.findViewById(R.id.parent_title);
            parentViewHolder = new ParentViewHolder(parent_cb, parent_title);
            convertView.setTag(parentViewHolder);
        } else {
            parentViewHolder = (ParentViewHolder) convertView.getTag();
        }
//        賦值
        Log.d(TAG, "getGroupView() returned: " + list.get(i).isParentIsSelected() + "---" + i);
        parentViewHolder.getParent_cb().setChecked(list.get(i).isParentIsSelected());
        parentViewHolder.getParent_title().setText(list.get(i).getSellerName());

//        checkbox的點擊事件
        parentViewHolder.getParent_cb().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean selected = list.get(i).isParentIsSelected();
                selected = !selected;
                list.get(i).setParentIsSelected(selected);
                //點擊父條目的複選框,子條目相應的改變
//                將複選框的狀態賦給子條目
                List<CartListBean.ChildBean> list1 = MyExpanableAdapter.this.list.get(i).getList();
                for (int x = 0; x < list1.size(); x++) {
                    CartListBean.ChildBean childBean = list1.get(x);
                    childBean.setChildIsSelected(selected);
                }
                notifyDataSetChanged();
                //計算
                String sum = SumUtils.sum(list);
                iView.showCount(sum);
            }

        });
        return convertView;
    }

    //獲取子分組視圖
    @Override
    public View getChildView(final int i, final int i1, boolean b, View convertView, ViewGroup viewGroup) {
        MyChildViewHolder myChildViewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.child_layout, null);
            CheckBox child_cb = convertView.findViewById(R.id.child_cb);
            ImageView child_icon = convertView.findViewById(R.id.child_icon);
            TextView child_price = convertView.findViewById(R.id.child_price);
            TextView child_title = convertView.findViewById(R.id.child_title);
            AddSubView addSubView = convertView.findViewById(R.id.add_sub_view);

            myChildViewHolder = new MyChildViewHolder(child_cb, child_title, child_price, addSubView, child_icon);
            convertView.setTag(myChildViewHolder);
        } else {
            myChildViewHolder = (MyChildViewHolder) convertView.getTag();
        }
        //賦值
        myChildViewHolder.getChildCb().setChecked(list.get(i).getList().get(i1).isChildIsSelected());
        myChildViewHolder.getChildTitle().setText(list.get(i).getList().get(i1).getTitle());
        myChildViewHolder.getChildPrice().setText(list.get(i).getList().get(i1).getPrice() + "");
        myChildViewHolder.getAddSubView().setCount(list.get(i).getList().get(i1).getNum());


//       設置加減按鈕的點擊事件
//        添加的監聽
        myChildViewHolder.getAddSubView().setOnAddClickListener(new AddSubView.AddClickListener() {
            @Override
            public void onAddClick(View view, int count) {
                list.get(i).getList().get(i1).setNum(count);
                //開始計算,如果選中就計算,沒選中,就不計算
                boolean selected = list.get(i).getList().get(i1).isChildIsSelected();
                if (selected) {
                    //計算
                    String sum = SumUtils.sum(list);
                    iView.showCount(sum);
                }
            }
        });

        //減的監聽
        myChildViewHolder.getAddSubView().setOnSubClickListener(new AddSubView.SubClickListener() {
            @Override
            public void onSubClick(View view, int count) {
                list.get(i).getList().get(i1).setNum(count);
                //開始計算,如果選中就計算,沒選中,就不計算
                boolean selected = list.get(i).getList().get(i1).isChildIsSelected();
                if (selected) {
                    //計算
                    String sum = SumUtils.sum(list);
                    iView.showCount(sum);
                }
            }
        });


        String images = list.get(i).getList().get(i1).getImages();
        String icon_url = images.split("\\|")[0];
        Glide.with(context).load(icon_url).into(myChildViewHolder.getChildIcon());

//        子分組的checkbox
        myChildViewHolder.getChildCb().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean childIsSelected = list.get(i).getList().get(i1).isChildIsSelected();
                childIsSelected = !childIsSelected;
                list.get(i).getList().get(i1).setChildIsSelected(childIsSelected);

                //當所有子條目被選中,所有父條目也被選中,如果有一個子條目沒選中,父條目就不能被選中
                List<CartListBean.ChildBean> list2 = MyExpanableAdapter.this.list.get(i).getList();
                boolean flag = true;
                for (int j = 0; j < list2.size(); j++) {
                    if (!list2.get(j).isChildIsSelected()) {
                        flag = false;
                    }
                }
                list.get(i).setParentIsSelected(flag);
                //刷新
                notifyDataSetChanged();
                //計算
                String sum = SumUtils.sum(list);
                iView.showCount(sum);
            }
        });
        return convertView;
    }

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

    //    父分組的viewholder
    class ParentViewHolder {
        private CheckBox parent_cb;
        private TextView parent_title;

        public ParentViewHolder(CheckBox parent_cb, TextView parent_title) {
            this.parent_cb = parent_cb;
            this.parent_title = parent_title;
        }

        public CheckBox getParent_cb() {
            return parent_cb;
        }

        public void setParent_cb(CheckBox parent_cb) {
            this.parent_cb = parent_cb;
        }

        public TextView getParent_title() {
            return parent_title;
        }

        public void setParent_title(TextView parent_title) {
            this.parent_title = parent_title;
        }

    }

    //創建子條目的viewholder
    class MyChildViewHolder {
        private CheckBox childCb;
        private TextView childTitle;
        private TextView childPrice;
        private AddSubView addSubView;
        private ImageView childIcon;

        public MyChildViewHolder(CheckBox childCb, TextView childTitle, TextView childPrice, AddSubView addSubView, ImageView childIcon) {
            this.childCb = childCb;
            this.childTitle = childTitle;
            this.childPrice = childPrice;
            this.addSubView = addSubView;
            this.childIcon = childIcon;
        }

        public CheckBox getChildCb() {
            return childCb;
        }

        public void setChildCb(CheckBox childCb) {
            this.childCb = childCb;
        }

        public TextView getChildTitle() {
            return childTitle;
        }

        public void setChildTitle(TextView childTitle) {
            this.childTitle = childTitle;
        }

        public TextView getChildPrice() {
            return childPrice;
        }

        public void setChildPrice(TextView childPrice) {
            this.childPrice = childPrice;
        }

        public AddSubView getAddSubView() {
            return addSubView;
        }

        public void setAddSubView(AddSubView addSubView) {
            this.addSubView = addSubView;
        }

        public ImageView getChildIcon() {
            return childIcon;
        }

        public void setChildIcon(ImageView childIcon) {
            this.childIcon = childIcon;
        }
    }
}

十八、utils包下的  SumUtils (計算價錢) 的工具類 


package com.bw.lenovo.goodscart.Utils;

import com.bw.lenovo.goodscart.model.CartListBean;

import java.util.List;

public class SumUtils {
    public static String sum(List<CartListBean.DataBean> list){
//定義一個int類型的變量
    int sum=0;

//for循環遍歷list集合
    for (int i=0;i<list.size();i++){
            List<CartListBean.ChildBean> list1 = list.get(i).getList();
            for (int j=0;j<list1.size();j++){
                CartListBean.ChildBean childBean = list1.get(j);
                if (childBean.isChildIsSelected()){
          //把單價乘以數量的的總價賦值給sum
               sum+=childBean.getPrice()*childBean.getNum();
                }
            }
        }
     return Double.toString(sum);
    }
}

十九、MainActivity的主頁面代碼


package com.bw.lenovo.goodscart;

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

import com.bw.lenovo.goodscart.Utils.SumUtils;
import com.bw.lenovo.goodscart.model.CartListBean;
import com.bw.lenovo.goodscart.model.CartModelImpl;
import com.bw.lenovo.goodscart.presenter.PresenterImpl;
import com.bw.lenovo.goodscart.view.IView;
import com.bw.lenovo.goodscart.view.MyExpanableAdapter;

import java.util.List;

public class MainActivity extends AppCompatActivity implements IView, View.OnClickListener {

    private static final String TAG = "MainActivity=======";
    private TextView sun;
    private ExpandableListView expan_list_view;
    private CheckBox xuan;
    private Button toJisuan;
    private List<CartListBean.DataBean> list;
    private MyExpanableAdapter adpter;

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

     //  初始化頁面
        initviews();

     //調用presenter
        PresenterImpl presenter=new PresenterImpl();
        presenter.showCartList(new CartModelImpl(),this);
    }

    private void initviews() {
        expan_list_view = (ExpandableListView) findViewById(R.id.expan_list_view);
        sun = (TextView) findViewById(R.id.sum);
        xuan = (CheckBox) findViewById(R.id.xuan);
        toJisuan = (Button) findViewById(R.id.toJisuan);
        xuan.setOnClickListener(this);
        toJisuan.setOnClickListener(this);
    }


    //    展示的回調
    @Override
    public void showCartList(List<CartListBean.DataBean> list) {
        this.list=list;
    // 展示數據
        Log.d(TAG, "showCartList() returned:" + list);
        Toast.makeText(MainActivity.this,list.toString(),Toast.LENGTH_LONG).show();
        adpter = new MyExpanableAdapter(MainActivity.this, list,this);
        expan_list_view.setAdapter(adpter);
        //展開字條目
        int groupCount = expan_list_view.getCount();

        for (int i = 0; i < groupCount; i++) {
            expan_list_view.expandGroup(i);
        }

    }

    //顯示總價
    @Override
    public void showCount(String count) {
        sun.setText("總價是:"+count);
        String sum = SumUtils.sum(list);
    }

    // 點擊事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //  全選。反選
            case R.id.xuan:
                boolean checked = xuan.isChecked();
                //只需要改變集合裏面的數據就可以啦!然後刷新
                for (int i=0;i<list.size();i++){
                    CartListBean.DataBean dataBean = list.get(i);
                    dataBean.setParentIsSelected(checked);
                    //獲得子集合  遍歷
                    List<CartListBean.ChildBean> list3 = dataBean.getList();

                    for (int j=0;j<list3.size();j++){
                      list3.get(j).setChildIsSelected(checked);
                    }
                }
                //刷新即可
                adpter.notifyDataSetChanged();
                String count = SumUtils.sum(list);
                sun.setText(count);
                break;
            //跳轉到結算頁面
            case R.id.toJisuan:
                Toast.makeText(this, "跳轉結算界面===", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

二十、自定義按鈕裏面的背景框(在drawable->Drawable resouce file)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size
    android:width="100dp"
    android:height="30dp"/>

<stroke
    android:width="1dp"
    android:color="#999999"/>
</shape>


 吐舌頭注:在子佈局導入自定義控件時,用自己包下的





發佈了53 篇原創文章 · 獲贊 8 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章