Android的购物车操作

来源:互联网 发布:浙江大学 网络服务中心 编辑:程序博客网 时间:2024/05/22 08:01


导入一个依赖可以计算价格
compile 'org.greenrobot:eventbus:3.1.1'


设置适配器
package com.bwie.newjingd.adapter;

import android.content.Context;
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 android.widget.Toast;

import com.bwie.newjingd.R;
import com.bwie.newjingd.bean.Car_Select;
import com.bwie.newjingd.presenter.Car_Select_Presenter;
import com.bwie.newjingd.v.Car_Select_View;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by 老北城 on 2017/12/18.
 */

public class MyCarShowAdapter extends BaseExpandableListAdapter implements Car_Select_View {
    private Context context;
    private List<Car_Select.DataBean> data;
    private List<List<Car_Select.DataBean.ListBean>> list;
    private Car_Select_Presenter car_select_presenter;
    private boolean isVisible;

    public MyCarShowAdapter(Context context, List<Car_Select.DataBean> data, List<List<Car_Select.DataBean.ListBean>> list) {
        this.context = context;
        this.data = data;
        this.list = list;
    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).get(childPosition);
    }

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

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

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

    //一级标题
    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.group1, null);
            holder.textView = (TextView) convertView.findViewById(R.id.g_title);
            holder.che = (CheckBox) convertView.findViewById(R.id.che);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(data.get(groupPosition).getSellerName());
        //设置一级列表checkBox的状态
        holder.che.setChecked(data.get(groupPosition).isChecked());
        //一级列表checkBox的点击事件
        holder.che.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断一级列表复选框的状态  设置为true或false
                data.get(groupPosition).setChecked(holder.che.isChecked());
                //改变二级checkbod的状态
                changeChildCbState(groupPosition, holder.che.isChecked());
                //算钱
                EventBus.getDefault().post(computer());
                //改变全选状态   isAllGroupCbSelect判断一级是否全部选中
                changeAllCbState(isAllGroupCbSelect());
                //必刷新
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    //二级标题
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final Car_Select.DataBean.ListBean listBean = list.get(groupPosition).get(childPosition);
        final ViewHolder2 holder2;
        if (convertView == null) {
            holder2 = new ViewHolder2();
            convertView = View.inflate(context, R.layout.chidren, null);
            holder2.z_title = (TextView) convertView.findViewById(R.id.z_title);
            holder2.z_che = (CheckBox) convertView.findViewById(R.id.z_che);
            holder2.img = (ImageView) convertView.findViewById(R.id.z_img);
            holder2.price = (TextView) convertView.findViewById(R.id.z_price);
            holder2.xiangqing = (TextView) convertView.findViewById(R.id.z_shuxing);
            holder2.jian = (ImageView) convertView.findViewById(R.id.iv_jian);
            holder2.jia = (ImageView) convertView.findViewById(R.id.iv_add);
            holder2.del = (ImageView) convertView.findViewById(R.id.del);
            holder2.num = (TextView) convertView.findViewById(R.id.tv_num);
            convertView.setTag(holder2);

        } else {
            holder2 = (ViewHolder2) convertView.getTag();
        }
        holder2.num.setText(list.get(groupPosition).get(childPosition).getNum() + "");
        holder2.z_title.setText(list.get(groupPosition).get(childPosition).getTitle());
        holder2.price.setText("¥" + list.get(groupPosition).get(childPosition).getPrice());
        holder2.xiangqing.setText(list.get(groupPosition).get(childPosition).getSubhead());
        String images = list.get(groupPosition).get(childPosition).getImages();
        String[] split = images.split("\\|");
        ImageLoader.getInstance().displayImage(split[0], holder2.img);
        //判断是否需要显示删除按钮
        if (!isVisible) {
            //隐藏
            holder2.del.setVisibility(View.GONE);
        } else {
            holder2.del.setVisibility(View.VISIBLE);
        }

        //设置二级列表checkbox的属性
        holder2.z_che.setChecked(list.get(groupPosition).get(childPosition).isChecked());
        //二级列表的点击事件
        holder2.z_che.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //设置该条目中的checkbox属性值
                listBean.setChecked(holder2.z_che.isChecked());
                //计算价钱
                PriceAndCountEvent priceAndCountEvent = computer();
                EventBus.getDefault().post(priceAndCountEvent);
                //判断当前checkbox是选中的状态
                if (holder2.z_che.isChecked()) {
                    //如果全部选中(isAllChildCbSelected)
                    if (isAllChildCbSelected(groupPosition)) {
                        //改变一级列表的状态
                        changeGroupCbState(groupPosition, true);
                        //改变全选的状态
                        changeAllCbState(isAllGroupCbSelect());
                    }
                } else {
                    //如果没有全部选中,一级列表的checkbox为false不为选中
                    changeGroupCbState(groupPosition, false);
                    changeAllCbState(isAllGroupCbSelect());
                }
                notifyDataSetChanged();
            }
        });

        //加号
        holder2.jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num = listBean.getNum();
                //num为int类型所以要加空字符串
                holder2.num.setText(++num + "");
                listBean.setNum(num);
                //如果二级列表的checkbox为选中,计算价钱
                if (holder2.z_che.isChecked()) {
                    PriceAndCountEvent priceAndCountEvent = computer();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });
        //减号
        holder2.jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num = listBean.getNum();
                if (num == 1) {
                    return;
                }
                holder2.num.setText(--num + "");
                listBean.setNum(num);
                if (holder2.z_che.isChecked()) {
                    PriceAndCountEvent priceAndCountEvent = computer();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });

        car_select_presenter = new Car_Select_Presenter(this);
        //点击删除购物车里面的内容
        holder2.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击传值
                car_select_presenter.delCar(list.get(groupPosition).get(childPosition).getPid() + "");
                List<Car_Select.DataBean.ListBean> listBeen = list.get(groupPosition);
                Car_Select.DataBean.ListBean remove = listBeen.remove(childPosition);
                if (listBeen.size() == 0) {
                    //先移除二级列表的集合,再移除一级列表的集合
                    list.remove(groupPosition);
                    data.remove(groupPosition);
                }
                //算钱
                EventBus.getDefault().post(computer());
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

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

    @Override
    public void onSuccess(List<Car_Select.DataBean> data) {

    }

    @Override
    public void onFailed(String message) {

    }

    @Override
    public void onDelSuccess(String msg) {
        Toast.makeText(context, msg + "", Toast.LENGTH_SHORT).show();
        notifyDataSetChanged();
    }

    @Override
    public void onDelFailed() {

    }


    class ViewHolder {
        TextView textView;
        CheckBox che;
    }

    class ViewHolder2 {
        ImageView img;
        TextView z_title;
        CheckBox z_che;
        TextView price;
        TextView xiangqing;
        ImageView jia;
        ImageView jian;
        ImageView del;
        TextView num;
    }

    //改变二级列表的checkbox的状态   如果一级选中,控制二级也选中
    private void changeChildCbState(int groupPosition, boolean flag) {
        List<Car_Select.DataBean.ListBean> listBeen = list.get(groupPosition);
        for (int j = 0; j < listBeen.size(); j++) {
            Car_Select.DataBean.ListBean listBean = listBeen.get(j);
            listBean.setChecked(flag);
        }
    }

    //判断一级列表是否全部选中
    public boolean isAllGroupCbSelect() {
        for (int i = 0; i < list.size(); i++) {
            Car_Select.DataBean dataBean = data.get(i);
            if (!dataBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    //改变全选的状态
    private void changeAllCbState(boolean flag) {
        MessageEvent messageEvent = new MessageEvent();
        messageEvent.setCheckd(flag);
        EventBus.getDefault().post(messageEvent);
    }

    //改变一级列表的checkbox的状态
    private void changeGroupCbState(int i, boolean flag) {
        Car_Select.DataBean dataBean = data.get(i);
        dataBean.setChecked(flag);
    }

    //判断二级列表是否全部选中
    private boolean isAllChildCbSelected(int i) {
        List<Car_Select.DataBean.ListBean> listBeen = list.get(i);
        for (int j = 0; j < listBeen.size(); j++) {
            Car_Select.DataBean.ListBean listBean = listBeen.get(j);
            if (!listBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    //设置全选,反选
    public void changeAllListCbState(boolean flag) {
        for (int i = 0; i < list.size(); i++) {
            changeGroupCbState(i, flag);
            changeChildCbState(i, flag);
        }
        //算钱
        EventBus.getDefault().post(computer());
        notifyDataSetChanged();
    }

    //计算列表的价钱
    private PriceAndCountEvent computer() {
        int count = 0;
        double price = 0;
        int to = 0;
        for (int i = 0; i < list.size(); i++) {
            List<Car_Select.DataBean.ListBean> listBeen = list.get(i);
            for (int j = 0; j < listBeen.size(); j++) {
                Car_Select.DataBean.ListBean listBean = listBeen.get(j);
                if (listBean.isChecked()) {
                    price += listBean.getNum() * listBean.getPrice();
                    count += listBean.getNum();
                    to += listBean.getNum();
                }
            }
        }
        PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
        priceAndCountEvent.setCount(count);
        priceAndCountEvent.setPrice(price);
        priceAndCountEvent.setTo(to);
        return priceAndCountEvent;
    }

    /**
     * 是否隐藏删除按钮
     *
     * @param isVisible
     * @return
     */
    public void showDelete(boolean isVisible) {
        this.isVisible = isVisible;
        notifyDataSetChanged();

    }
}

一级标题的布局
<?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">
    <CheckBox
        android:layout_marginTop="20dp"
        android:id="@+id/che"
        android:layout_width="25dp"
        android:layout_height="25dp" />

    <TextView
        android:layout_marginTop="20dp"
        android:id="@+id/g_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="------------" />
</LinearLayout>

耳机列表的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <CheckBox
        android:layout_marginLeft="20dp"
        android:id="@+id/z_che"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginTop="11dp" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_toRightOf="@id/z_che"
        android:orientation="vertical">

        <TextView
            android:id="@+id/z_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="三只松鼠大礼包"
            android:textSize="16sp"
            android:textStyle="bold" />

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

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

            <TextView
                android:id="@+id/z_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/z_img"
                android:layout_toRightOf="@+id/z_img"
                android:layout_marginLeft="5dp"
                android:text="价钱"
                android:textColor="#ff0000" />

            <TextView
                android:id="@+id/z_shuxing"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/z_price"
                android:layout_toEndOf="@+id/z_img"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@+id/z_img"
                android:layout_marginTop="5dp"
                android:textSize="14sp"
                android:textColor="@color/colorPrimary"
                android:text="价钱" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/z_shuxing"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="20dp"
                android:layout_toRightOf="@id/z_img"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/iv_jian"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:src="@drawable/shopcart_minus_red" />

                <TextView
                    android:id="@+id/tv_num"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:textSize="20sp"
                    android:text="1" />

                <ImageView
                    android:id="@+id/iv_add"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_marginLeft="5dp"
                    android:src="@drawable/shopcart_add_red" />

                <ImageView
                    android:id="@+id/del"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_gravity="right"
                    android:layout_weight="1"
                    android:src="@drawable/rublish" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

主页面的布局
<?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:background="#EBEBEB"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#ffffff"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:padding="10dp"
            android:id="@+id/btnBack"
            android:text="返回"/>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="购物车"
            android:textSize="25sp"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="10dp"
            android:layout_gravity="center_horizontal"/>

        <TextView
            android:padding="10dp"
            android:textSize="25sp"
            android:id="@+id/btnEditor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="编辑"/>

    </LinearLayout>

    <ExpandableListView
        android:background="#fff"
        android:id="@+id/exlv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"></ExpandableListView>

    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff">

        <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/qx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/checkbox"
            android:text="全选" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/qx"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#ff0000"
                android:textSize="16sp"
                android:text="总价:0.0" />

            <TextView
                android:layout_marginLeft="30dp"
                android:id="@+id/num"
                android:textColor="#ff0000"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="共0件商品" />

        </LinearLayout>

        <Button
            android:id="@+id/jiesuan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="#ff0000"
            android:text="去结算(0)" />

    </RelativeLayout>

</LinearLayout>


需要的类
package com.bwie.newjingd.adapter;

/**
 * Created by HASEE on 2017/11/22.
 */

public class MessageEvent {
    private boolean checkd;
    public boolean isCheckd(){
        return checkd;
    }
    public void  setCheckd(boolean checkd){
        this.checkd=checkd;
    }
}

package com.bwie.newjingd.adapter;

/**
 * Created by HASEE on 2017/11/22.
 */

public class PriceAndCountEvent {
    private double price;
    private int count;
    private int to;

    public double getPrice() {
        return price;
    }

    public int getCount() {
        return count;
    }

    public int getTo(){
        return  to;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setTo(int to) {
        this.to = to;
    }
}


然后在bean里面加东西
public boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

private boolean checked;




原创粉丝点击