1. 程式人生 > >購物車(二級列表ExPandableListView)

購物車(二級列表ExPandableListView)

MainActivity

package com.zjh.administrat.shoppcarexpandablelistview.view;

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

import com.zjh.administrat.shoppcarexpandablelistview.R;
import com.zjh.administrat.shoppcarexpandablelistview.adapter.MyAdapter;
import com.zjh.administrat.shoppcarexpandablelistview.bean.ShoppingBean;
import com.zjh.administrat.shoppcarexpandablelistview.presenter.IPresenterImpl;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements IView {

    private String urlStr = "http://www.zhaoapi.cn/product/getCarts";
    private IPresenterImpl iPresenter;
    private ExpandableListView mListView;
    private CheckBox selectAll;
    private TextView priceAll;
    private Button numberAll;
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        //建立我的介面卡
        mAdapter = new MyAdapter(this);
        mListView.setAdapter(mAdapter);
    }

    private void initView() {
        mListView = findViewById(R.id.listView);
        selectAll = findViewById(R.id.selectAll);
        priceAll = findViewById(R.id.priceAll);
        numberAll = findViewById(R.id.numberAll);

        //全選按鈕
        selectAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //底部全選按鈕選中的時候所有得商品都被選中
                boolean allSelected = mAdapter.isAllSelected();
                mAdapter.changeAllStatus(!allSelected);
                mAdapter.notifyDataSetChanged();
                //重新整理底部資料
                refreshData();
            }
        });

    }
    //重新整理的方法  checkBox, 總價, 總數量
    private void  refreshData(){
        //判斷所有商品是否被選中
        boolean allSelected = mAdapter.isAllSelected();
        //設定給全選
        selectAll.setSelected(allSelected);
        //計算總價
        float totalPrice = mAdapter.totalPrice();
        priceAll.setText("總價:¥"+totalPrice);
        //計算總數量
        int totalNumber = mAdapter.totalNumber();
        numberAll.setText("去結算("+totalNumber+")");

    }

    //處理資料
    private void initData() {
        iPresenter = new IPresenterImpl(this);
        Map<String, String> map = new HashMap<>();
        map.put("uid", "71");
        iPresenter.pRequestData(urlStr, map, ShoppingBean.class);
    }


    //返回資料i
    @Override
    public void viewData(Object object) {
        ShoppingBean shoppingBean = (ShoppingBean) object;
        mAdapter.setDataAll(shoppingBean.getData());

        //展開二級列表
        for (int p = 0; p < shoppingBean.getData().size(); p++) {
            mListView.expandGroup(p);
        }

        //回撥處理
        mAdapter.setOnCartListChangeListener(new MyAdapter.OnCartListChangeListener() {
            @Override
            public void onParentCheckedChange(int i) {
                //點選商家複選框
                boolean parentSelected = mAdapter.isParentSelected(i);
                mAdapter.changeParentAllStatus(i, !parentSelected);
                mAdapter.notifyDataSetChanged();
                //重新整理底部資料
                refreshData();
            }

            @Override
            public void onChildCheckedChange(int i, int i1) {
                //點選商品複選框
                mAdapter.changeChildAllStatus(i, i1);
                mAdapter.notifyDataSetChanged();
                //重新整理底部資料
                refreshData();
            }

            @Override
            public void onNumberChange(int i, int i1, int number) {
                //點選加減器
                mAdapter.changeNumber(i, i1, number);
                mAdapter.notifyDataSetChanged();
                //重新整理底部資料
                refreshData();
            }
        });
    }

    //處理記憶體
    @Override
    protected void onDestroy() {
        super.onDestroy();
        iPresenter.onDetch();
    }
}

MyAdapter

package com.zjh.administrat.shoppcarexpandablelistview.adapter;

import android.content.Context;
import android.view.LayoutInflater;
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.zjh.administrat.shoppcarexpandablelistview.R;
import com.zjh.administrat.shoppcarexpandablelistview.bean.ShoppingBean;
import com.zjh.administrat.shoppcarexpandablelistview.view.AddSubView;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends BaseExpandableListAdapter {
    private List<ShoppingBean.DataBean> list;
    private Context mContext;

    public MyAdapter(Context mContext) {
        this.mContext = mContext;
        list = new ArrayList<>();
    }

    //資料料理
    public void setDataAll(List<ShoppingBean.DataBean> data) {
        if (data != null) {
            list.addAll(data);
        }
        notifyDataSetChanged();
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getList().size();
    }

    //商家------------
    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentHolder parentHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_cart_parent, parent, false);
            parentHolder = new ParentHolder(convertView);
        } else {
            parentHolder = (ParentHolder) convertView.getTag();
        }
        parentHolder.parent_title.setText(list.get(groupPosition).getSellerName());
        boolean parentSelected = isParentSelected(groupPosition);
        parentHolder.parent_checkBox.setChecked(parentSelected);
        //設定點選商家的checkBox,
        parentHolder.parent_checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 if (mOnCartListChangeListener != null){
                     mOnCartListChangeListener.onParentCheckedChange(groupPosition);
                 }
            }
        });
        return convertView;
    }

    //商品-----------------------
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder childHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_cart_child, parent, false);
            childHolder = new ChildHolder(convertView);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        //設定商品圖片***
        String str = "";
        int length = list.get(groupPosition).getList().get(childPosition).getImages().length();
        for (int j = 0; j < length; j++) {
            if (list.get(groupPosition).getList().get(childPosition).getImages().substring(j, j + 1).equals("|")) {
                str = list.get(groupPosition).getList().get(childPosition).getImages().substring(j + 1, length).trim();
            }
        }
        Glide.with(mContext).load(str).into(childHolder.child_icon);
        // 設定商品名稱***
        childHolder.child_title.setText(list.get(groupPosition).getList().get(childPosition).getTitle());
        // 設定商品價格***
        childHolder.child_price.setText("¥"+list.get(groupPosition).getList().get(childPosition).getPrice() + "");
        //設定chebox複選框是否選中
        childHolder.child_checkBox.setChecked(list.get(groupPosition).getList().get(childPosition).getSelected() == 1);
        //設定自定義控制元件中的數量
        childHolder.add_remove_view.setNumber(list.get(groupPosition).getList().get(childPosition).getNum());

        //設定商品checkBox點選事件, 通過介面回撥,暴露給外面
        childHolder.child_checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnCartListChangeListener != null){
                    mOnCartListChangeListener.onChildCheckedChange(groupPosition, childPosition);
                }
            }
        });
        //設定數量點選事件, 通過介面回撥,暴露給外面
        childHolder.add_remove_view.setNumberChangeListener(new AddSubView.OnNumberChangeListener() {
            @Override
            public void OnNumberChange(int number) {
                if (mOnCartListChangeListener != null){
                    mOnCartListChangeListener.onNumberChange(groupPosition, childPosition, number);
                }
            }
        });
        return convertView;
    }

    //定義判斷複選框的boolean狀態--------------------------------------
    //建立商家的複選框方法  //遍歷所有商品複選框  //判斷商家選中狀態
    public boolean isParentSelected(int i) {
        List<ShoppingBean.DataBean.ListBean> getList = this.list.get(i).getList();
        for (ShoppingBean.DataBean.ListBean bean : getList) {
            if (bean.getSelected() == 0) {
                return false;
            }
        }
        return true;
    }
    //下方的全選/全不選按鈕的狀態  //遍歷所有的框
    public boolean isAllSelected(){
        for(int s = 0; s < list.size(); s++){
            List<ShoppingBean.DataBean.ListBean> listAll = this.list.get(s).getList();
            for (int j = 0; j < listAll.size(); j++){
                if (listAll.get(j).getSelected() == 0){
                    return false;
                }
            }
        }
        return true;
    }
    //計算總數量  遍歷 求所有商品數量的和
    public int totalNumber(){
        int totalNumber = 0;
        for (int i = 0; i < list.size(); i++) {
            List<ShoppingBean.DataBean.ListBean> list2 = this.list.get(i).getList();
            for (int j = 0; j < list2.size(); j++) {
                if (list2.get(j).getSelected() == 1){
                    int num = list2.get(j).getNum();
                    totalNumber += num;
                }
            }
        }
        return totalNumber;
    }
    //計算總價格  遍歷 求所有商品價格的和
    public float totalPrice(){
        int totalPrice = 0;
        for (int i = 0; i < list.size(); i++) {
            List<ShoppingBean.DataBean.ListBean> list3 = this.list.get(i).getList();
            for (int j = 0; j < list3.size(); j++) {
                if (list3.get(j).getSelected() == 1){
                    double price = list3.get(j).getPrice();
                    int num = list3.get(j).getNum();
                    totalPrice += num * price;
                }
            }
        }
        return totalPrice;
    }
    //商家選中時  更新checkBox的狀態
    public void changeParentAllStatus(int i, boolean isSelected){
        List<ShoppingBean.DataBean.ListBean> beans = this.list.get(i).getList();
        for (int j = 0; j < beans.size(); j++) {
            ShoppingBean.DataBean.ListBean bean = beans.get(j);
            bean.setSelected(isSelected ? 1 : 0);
        }
    }
    //當商品選中時   更新checkbox狀態
    public void changeChildAllStatus(int i, int i1){
        ShoppingBean.DataBean.ListBean beans = list.get(i).getList().get(i1);
        beans.setSelected(beans.getSelected()==0 ? 1 : 0);
    }
    //*設定所有的checkBox的狀態
    public void changeAllStatus(boolean isSelected){
        for (int i = 0; i < list.size(); i++) {
            List<ShoppingBean.DataBean.ListBean> beans = this.list.get(i).getList();
            for (int j = 0; j < beans.size() ; j++) {
                beans.get(j).setSelected(isSelected ? 1 : 0);
            }
        }
    }
    //*當加減器被點選
    public void changeNumber(int i, int i1, int number){
        ShoppingBean.DataBean.ListBean beans = list.get(i).getList().get(i1);
        beans.setNum(number);
    }

    //*************商家的ViewHolder----------------------------------------------------
    class ParentHolder {
        CheckBox parent_checkBox;
        TextView parent_title;

        public ParentHolder(View itemView) {
            parent_checkBox = itemView.findViewById(R.id.parent_checkBox);
            parent_title = itemView.findViewById(R.id.parent_title);
            itemView.setTag(this);
        }
        //繫結資料寫在holder中。
    }

    //商品------------------------------
    class ChildHolder {
        CheckBox child_checkBox;
        ImageView child_icon;
        TextView child_title, child_price;
        AddSubView add_remove_view;

        private ChildHolder(View itemView) {
            child_checkBox = itemView.findViewById(R.id.child_checkBox);
            child_icon = itemView.findViewById(R.id.child_icon);
            child_title = itemView.findViewById(R.id.child_title);
            child_price = itemView.findViewById(R.id.child_price);
            add_remove_view = itemView.findViewById(R.id.add_remove_view);
            itemView.setTag(this);
        }
        //繫結資料寫在holder中。
    }

    //向activity暴露資料的介面
    //成員變數
    OnCartListChangeListener mOnCartListChangeListener;
    //方法的set方法
    public void setOnCartListChangeListener(OnCartListChangeListener onCartListChangeListener) {
        mOnCartListChangeListener = onCartListChangeListener;
    }
    public interface OnCartListChangeListener {
        //當商家的checkBox點選時回撥 **
        void onParentCheckedChange(int i);
        //當商品的CheckBox點選時回撥 **
        void onChildCheckedChange(int i, int i1);
        //當點選加減按鈕的回撥 ***
        void onNumberChange(int i, int i1, int number);
    }

        //以下的 不需要使用============================
        @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;
        }
    }

AddSubView自定義(加減)

package com.zjh.administrat.shoppcarexpandablelistview.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zjh.administrat.shoppcarexpandablelistview.R;

public class AddSubView extends LinearLayout implements View.OnClickListener {
    private int number = 1;
    private TextView number_jian,number_number, number_add;

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

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

    public AddSubView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = inflate(context, R.layout.add_remove_number, this);
        number_jian = view.findViewById(R.id.number_jian);
        number_number = view.findViewById(R.id.number_number);
        number_add = view.findViewById(R.id.number_add);

        number_jian.setOnClickListener(this);
        number_add.setOnClickListener(this);
    }

    //點選事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.number_jian:
                if (number > 1) {
                    -- number;
                    number_number.setText(number + "");
                    if (mNumberChangeListener != null) {
                        mNumberChangeListener.OnNumberChange(number);
                    }
                } else {
                    Toast.makeText(getContext(), "我是有底線的!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.number_add:
                if (number < 9) {
                    ++ number;
                    number_number.setText(number+"");
                    if (mNumberChangeListener != null) {
                        mNumberChangeListener.OnNumberChange(number);
                    }
                } else {
                    Toast.makeText(getContext(), "每人限購9件!", Toast.LENGTH_SHORT).show();
                }
            default:
                break;
        }
    }

    public int getNumber() {
        return number;
    }

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

    //成員變數
    OnNumberChangeListener mNumberChangeListener;
    //set方法

    public void setNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
        this.mNumberChangeListener = onNumberChangeListener;
    }

    //定義個介面
    public interface OnNumberChangeListener{
        void OnNumberChange(int number);
    }
}

以下的佈局

activity_main.xml主佈局

<?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=".view.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#fffc"
        android:gravity="center"
        android:text="購 物 車"
        android:textColor="#f00"
        android:textSize="30sp"
        android:textStyle="bold" />

    <ExpandableListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="9" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_weight="1"
        android:background="#94cc"
        android:gravity="center_vertical">

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

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

        <TextView
            android:id="@+id/priceAll"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="20dp"
            android:text="合計:¥0.00"
            android:textColor="#f00"
            android:textSize="30sp" />

        <Button
            android:id="@+id/numberAll"
            android:layout_width="120dp"
            android:layout_height="match_parent"
            android:background="#cc4"
            android:text="去結算[0]"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

item_cart_parent.xml商家

<?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="match_parent"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/parent_checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"/>

    <TextView
        android:id="@+id/parent_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textStyle="bold"
        android:gravity="center"
        />

</LinearLayout>

item_cart_child.xml商品

<?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:orientation="horizontal">

    <CheckBox
        android:id="@+id/child_checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />

    <ImageView
        android:id="@+id/child_icon"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:scaleType="centerCrop"
        />

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

        <TextView
            android:id="@+id/child_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="資料"
            android:textSize="25sp"
            android:maxLines="2"
            />

        <TextView
            android:id="@+id/child_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="¥0.0"
            android:textSize="25sp"
            android:textStyle="bold"
            android:textColor="#f00"
            />

    </LinearLayout>

    <com.zjh.administrat.shoppcarexpandablelistview.view.AddSubView
        android:id="@+id/add_remove_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>


</LinearLayout>

add_remove_number.xml加減器

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="70dp"
    android:layout_height="30dp"
    android:background="#99fff0"
    android:padding="3dp"
    android:orientation="horizontal">

  <TextView
      android:id="@+id/number_jian"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:background="#fcf4ff"
      android:layout_weight="1"
      android:textSize="25sp"
      android:gravity="center"
      android:text="-"
      />
    <TextView
        android:id="@+id/number_number"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="#44cc"
        android:layout_weight="2"
        android:text="1"
        android:padding="1dp"
        android:textSize="20sp"
        android:gravity="center"
        />
    <TextView
        android:id="@+id/number_add"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="#fcf4ff"
        android:layout_weight="1"
        android:textSize="25sp"
        android:gravity="center"
        android:text="+"
        />
</LinearLayout>

購物車1