1. 程式人生 > >購物車中的操作

購物車中的操作

效果
在這裡插入圖片描述
一、MainActivty佈局

	<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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <ExpandableListView
        android:id="@+id/expandableLV"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="15dp">

        <CheckBox
            android:id="@+id/isCheckAll_main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全選"
            android:padding="15dp"/>

        <TextView
            android:id="@+id/goodsPrice_main"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:text="商品總價:¥ 0 元"
            android:textSize="20sp"
            android:textColor="#000"/>

        <Button
            android:id="@+id/closing_main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="結算(0)"/>
    </LinearLayout>

</LinearLayout>

需要2個佈局一個商家的佈局一個商品的佈局
商家

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/goods_addck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginLeft="35dp"
        android:focusable="false"/>

    <TextView
        android:id="@+id/goods_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="商家1"
        android:textColor="#000"
        android:textSize="25sp"
        android:padding="15dp"/>

</LinearLayout>

商品

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/child_ck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:layout_marginLeft="55dp"
        android:layout_gravity="center"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center"/>

    <LinearLayout
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:padding="15dp">

        <TextView
            android:id="@+id/child_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="簡介"/>

        <TextView
            android:id="@+id/child_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="價錢"/>

    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <com.wjh.wangjiahui.view.Subtractor
            android:id="@+id/child_subtractor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:layout_alignParentRight="true"/>

    </RelativeLayout>
</LinearLayout>

需要一個自定義的佈局
選用組合式自定義佈局
在這裡插入圖片描述
建立開始

/**
 * date:2018/11/21
 * author:王加輝(家輝輝輝)
 * function:加減器自定義控制元件
 */
public class Subtractor extends LinearLayout implements View.OnClickListener {

    private TextView numberText;

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

    public Subtractor(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public Subtractor(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //初始化操作
        initView(context);

    }

    private void initView(Context context) {
        //載入自定義控制元件佈局
        View view = View.inflate(context, R.layout.subtractor_item, this);
        //找控制元件
        Button jian = view.findViewById(R.id.jian_subItem);
        Button jia = view.findViewById(R.id.jia_subItem);
        numberText = view.findViewById(R.id.number_subItem);

        //非常需要注意的一步,如果不寫點選事件沒效果
        jian.setOnClickListener(this);
        jia.setOnClickListener(this);

    }

    private int number = 1;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.jian_subItem://-
                if(number > 1 ){
                    -- number ;
                    numberText.setText(number + "");
                    //往介面中存資料
                    if(mSubtractorInterface != null){
                        mSubtractorInterface.setNumberListener(number);
                    }
                }else{
                    Toast.makeText(getContext(), "不能再少了", Toast.LENGTH_SHORT).show();
                }
            break;
            case R.id.jia_subItem://+
                if(number < 10){
                    ++ number;
                    numberText.setText(number +"");
                    //往介面中存資料
                    if(mSubtractorInterface != null){
                        mSubtractorInterface.setNumberListener(number);
                    }
                }else{
                    Toast.makeText(getContext(), "別加了親,爆了", Toast.LENGTH_SHORT).show();
                }
            break;
        }
    }

    //建立介面
    public interface SubtractorInterface{
        void setNumberListener(int num);
    }

    //宣告介面
    private SubtractorInterface mSubtractorInterface;

    //暴露方法
    public void setSubtractorInterface(SubtractorInterface subtractorInterface){
        mSubtractorInterface = subtractorInterface;
    }

    //很重要的一步set、get方法
    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
        numberText.setText(""+number);
    }
}

自定義佈局Iten

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <Button
        android:id="@+id/jian_subItem"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="-"
        android:textSize="25sp"/>

    <TextView
        android:id="@+id/number_subItem"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:padding="15dp"
        android:gravity="center"
        android:text="1"
        android:textSize="15sp"
        android:textColor="#000"/>

    <Button
        android:id="@+id/jia_subItem"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="+"
        android:textSize="25sp"/>
</LinearLayout>

okHttp的工具類封裝就不寫了
下面介面卡

/**
 * date:2018/11/21
 * author:王加輝(家輝輝輝)
 * function:BaseExpandableListAdapter介面卡
 */
public class MyExLVAdapter extends BaseExpandableListAdapter {

    private List<NetWorkDataBean.DataBean> mData;
    private Context mContext;

    public MyExLVAdapter(List<NetWorkDataBean.DataBean> data, Context context) {
        mData = data;
        mContext = context;
    }

    @Override
    public int getGroupCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mData.get(groupPosition) == null ? 0 : mData.get(groupPosition).getList().size();
    }



    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder_group vh_group ;
        if(convertView == null){
            convertView = View.inflate(parent.getContext(), R.layout.group_item,null);
            vh_group = new ViewHolder_group(convertView);
        }else{
            vh_group = (ViewHolder_group) convertView.getTag();
        }

        //設定內容
        NetWorkDataBean.DataBean dataBean = mData.get(groupPosition);
        vh_group.setData(dataBean,groupPosition);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ViewHolder vh_child ;

        if(convertView == null){
            convertView = View.inflate(parent.getContext(),R.layout.child_item,null);
            vh_child = new ViewHolder(convertView);
        }else {
            vh_child = (ViewHolder) convertView.getTag();
        }

        //新增資料
        List<NetWorkDataBean.DataBean.ListBean> list = mData.get(groupPosition).getList();
        NetWorkDataBean.DataBean.ListBean listBean = list.get(childPosition);
        vh_child.setList(listBean,groupPosition,childPosition);
        return convertView;
    }

    //優化
    class ViewHolder_group{
        CheckBox addCk;
        TextView gName;

        //構造方法
        public ViewHolder_group(View viewItem){
            addCk = viewItem.findViewById(R.id.goods_addck);
            gName = viewItem.findViewById(R.id.goods_name);
            viewItem.setTag(this);
        }

        //設定內容
        public void setData(NetWorkDataBean.DataBean dataBean, final int groupPosition){
            //店名
            gName.setText(dataBean.getSellerName());

            //根據當前所有的商品,判斷商家CheckBox是否選中
            boolean isCheckbox = changeGoodsCk(groupPosition);

            //根據 isCheckbox 設定是否選中
            addCk.setChecked(isCheckbox);

            //點選事件
            addCk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mMyExLvaInterface.gooderCheckBoxListener(groupPosition);
                }
            });

        }
    }

    //子
    class ViewHolder{
        CheckBox ck;
        TextView name,price;
        Subtractor subtractor;
        ImageView image;
        private String substring;

        public ViewHolder(View viewItem){
            ck = viewItem.findViewById(R.id.child_ck);
            name = viewItem.findViewById(R.id.child_name);
            price = viewItem.findViewById(R.id.child_price);
            subtractor = viewItem.findViewById(R.id.child_subtractor);
            image = viewItem.findViewById(R.id.imageView);
            viewItem.setTag(this);
        }

        public void setList(NetWorkDataBean.DataBean.ListBean listBean, final int groupPosition, final int childPosition){

            String images = listBean.getImages();

            if(images.indexOf("|") != -1){
                substring = images.substring(0, images.indexOf("|"));
                Picasso.with(mContext).load(substring).into(image);
            }else{
                Picasso.with(mContext).load(substring).into(image);
            }

            //因為資料中有選中的資料
            ck.setChecked(listBean.getSelected() == 1);
            name.setText(listBean.getTitle());
            price.setText(listBean.getPrice() + "");

            //為自定義控制元件設定數量
            subtractor.setNumber(listBean.getNum());
            //點選事件
            ck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mMyExLvaInterface.goodsCheckBoxListener(groupPosition,childPosition);
                }
            });

            //回撥自定義控制元件
            subtractor.setSubtractorInterface(new Subtractor.SubtractorInterface() {
                @Override
                public void setNumberListener(int num) {
                    mMyExLvaInterface.subChangeListener(groupPosition,childPosition,num);
                }
            });


        }

    }

    //購物車最外面一層的邏輯
    //判斷商家的單選框的狀態
    public boolean changeGoodsCk(int groupPosition){

        NetWorkDataBean.DataBean dataBean = mData.get(groupPosition);

        List<NetWorkDataBean.DataBean.ListBean> list = dataBean.getList();

        for (NetWorkDataBean.DataBean.ListBean listBean : list) {
            int selected = listBean.getSelected();
            //如果有一個商品未選中那麼就商家單選框就未選中
            if(selected == 0){
                return false;
            }
        }
        return true;
    }

    //判斷底部單選框的狀態
    public boolean changeBottom(){
        for(int x=0;x<mData.size();x++){
            List<NetWorkDataBean.DataBean.ListBean> list = mData.get(x).getList();
            for(int j=0 ; j<list.size();j++){
                NetWorkDataBean.DataBean.ListBean listBean = list.get(j);
                if(listBean.getSelected() == 0){
                    return false;
                }
            }
        }
        return true;
    }

    //計算商品的總價
    public float countPrice(){

        float sumPrice = 0;

        for(int x=0;x<mData.size();x++){
            List<NetWorkDataBean.DataBean.ListBean> list = mData.get(x).getList();
            for(int j=0;j<list.size();j++){
                NetWorkDataBean.DataBean.ListBean listBean = list.get(j);
                if(listBean.getSelected() == 1){
                    //得到數量和單價
                    int num = listBean.getNum();
                    float price = listBean.getPrice();
                    //計算
                    sumPrice += num*price;
                }

            }
        }
        return sumPrice;
    }

    //獲取商品的數量
    public int goodsNum(){
        int sumNum = 0;
        for(int x=0;x<mData.size();x++){
            List<NetWorkDataBean.DataBean.ListBean> list = mData.get(x).getList();
            for(int j=0;j<list.size();j++){
                if(list.get(j).getSelected() == 1){
                    int num = list.get(j).getNum();
                    sumNum += num ;
                }
            }
        }
        return sumNum;
    }

    /*
    * 根據使用者的互動,改變複選框的顯示
    * */
    //根據商家單選框改變商品的單選框
    public void changeGoods_GoodsAllCk(int groupPosition,boolean isCheckbox){
        List<NetWorkDataBean.DataBean.ListBean> list = mData.get(groupPosition).getList();
        //遍歷所有的商品
        for(int x=0;x<list.size();x++){
            NetWorkDataBean.DataBean.ListBean listBean = list.get(x);
            listBean.setSelected(isCheckbox ? 1 : 0);
        }
    }

    //根據商家子條目的全部點選改變商家的單選框狀態
    public void changeGoods_GooderCk(int groupPosition,int childPosition){
        List<NetWorkDataBean.DataBean.ListBean> list = mData.get(groupPosition).getList();
        NetWorkDataBean.DataBean.ListBean listBean = list.get(childPosition);
        listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0);
    }

    //底部全選框
    public void changeBottomAllCk(boolean selected){
        for(int x=0;x<mData.size();x++){
            List<NetWorkDataBean.DataBean.ListBean> list = mData.get(x).getList();
            for(int j=0;j<list.size();j++){
                NetWorkDataBean.DataBean.ListBean listBean = list.get(j);
                listBean.setSelected(selected ? 1: 0);
            }
        }
    }

    //點選加減號的時候改變裡面的值
    public void changeSubValue(int groupPosition,int childPosition,int num){
        List<NetWorkDataBean.DataBean.ListBean> list = mData.get(groupPosition).getList();
        NetWorkDataBean.DataBean.ListBean listBean = list.get(childPosition);
        listBean.setNum(num);
    }

    //建立介面
    public interface MyExLvaInterface{
        /**
         * 當商家的checkBox點選的時候回撥
         * */
        void gooderCheckBoxListener(int groupPosition);

        /**
         * 當商品的CheckBox點選時候回撥
         * */
        void goodsCheckBoxListener(int groupPosition,int childPosition);

        /**
         * 當點選加減號的時候回撥
         * */
        void subChangeListener(int groupPosition,int childPosition,int number);
    }

    //宣告介面
    private MyExLvaInterface mMyExLvaInterface;

    //暴露方法
    public void setMyExLvaInterface(MyExLvaInterface myExLvaInterface){
        mMyExLvaInterface = myExLvaInterface;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

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

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

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

MainActivity程式碼

public class MainActivity extends AppCompatActivity {

    private ExpandableListView mExpandableListView;
    private CheckBox mIsCheckAll;
    private TextView mGoodsPrice;
    private Button mClosing;
    private String strUrl = "http://www.zhaoapi.cn/product/getCarts";
    private MyExLVAdapter mMAdapter;

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

        //找控制元件
        initView();

        //初始化資料
        initData();

    }
     /*
     *  載入資料
     * */
    private void initData() {

        //建立Presenter層
        GoodsPresenter presenter = new GoodsPresenter();

        //建立Map集合
        HashMap<String, String> map = new HashMap<>();
        map.put("uid","71");
        presenter.NetWorkModel(strUrl, map, new OkHttpUtilsInterface() {
            @Override
            public void failure(Exception e) {
                Toast.makeText(MainActivity.this, "資料載入錯誤", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void success(String json) {
                NetWorkDataBean netWorkDataBean = new Gson().fromJson(json, NetWorkDataBean.class);
                //判斷請求是否成功
                if("0".equals(netWorkDataBean.getCode())){
                    List<NetWorkDataBean.DataBean> data = netWorkDataBean.getData();
                    //設定介面卡
                    mMAdapter = new MyExLVAdapter(data, MainActivity.this);

                    //先更新ui
                    changeAllUi();

                    //回撥介面
                    mMAdapter.setMyExLvaInterface(new MyExLVAdapter.MyExLvaInterface() {
                        @Override
                        public void gooderCheckBoxListener(int groupPosition) {
                            boolean changeGoodsCk = mMAdapter.changeGoodsCk(groupPosition);
                            mMAdapter.changeGoods_GoodsAllCk(groupPosition,!changeGoodsCk);
                            mMAdapter.notifyDataSetChanged();
                            changeAllUi();
                        }

                        @Override
                        public void goodsCheckBoxListener(int groupPosition, int childPosition) {
                            mMAdapter.changeGoods_GooderCk(groupPosition,childPosition);
                            mMAdapter.notifyDataSetChanged();
                            changeAllUi();
                        }

                        @Override
                        public void subChangeListener(int groupPosition, int childPosition, int number) {
                            mMAdapter.changeSubValue(groupPosition,childPosition,number);
                            mMAdapter.notifyDataSetChanged();
                            changeAllUi();
                        }
                    });

                    mExpandableListView.setAdapter(mMAdapter);

                    for(int x=0 ; x<data.size();x++){
                        mExpandableListView.expandGroup(x);
                    }

                }else{
                    Toast.makeText(MainActivity.this, "請求網路失敗", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    /*
     *  初始化控制元件
     * */
    private void initView() {
        mExpandableListView = findViewById(R.id.expandableLV);
        mIsCheckAll = findViewById(R.id.isCheckAll_main);
        mGoodsPrice = findViewById(R.id.goodsPrice_main);
        mClosing = findViewById(R.id.closing_main);
        //結算點選事件
        mClosing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "恭喜你,沒結算成功", Toast.LENGTH_SHORT).show();
            }
        });

        //全選
        mIsCheckAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean bottomCK = mMAdapter.changeBottom();
                mMAdapter.changeBottomAllCk(!bottomCK);
                mMAdapter.notifyDataSetChanged();
                changeAllUi();
            }
        });
    }

    //重新整理的方法
    private void changeAllUi(){
        //判斷底部是否全部點選
        boolean bottom = mMAdapter.changeBottom();
        mIsCheckAll.setChecked(bottom);

        //計算總價
        float price = mMAdapter.countPrice();
        mGoodsPrice.setText("總價:¥"+price+"元");

        //計算數量
        int goodsNum = mMAdapter.goodsNum();
        mClosing.setText("結算("+goodsNum+")");
    }
}