Android 一个简单的购物车

主页面的布局文件
<ExpandableListView
        android:id="@+id/exp_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@null"
        ></ExpandableListView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="9"
        android:gravity="center"
        android:orientation="horizontal">

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

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

        <TextView
            android:id="@+id/sum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            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>
主页面
public class GouWuCheActivity extends AppCompatActivity implements IGouWuCheIView, View.OnClickListener {
    private static final String TAG = "GouWuCheActivity";
    @BindView(R.id.exp_listview)
    ExpandableListView expListview;
    @BindView(R.id.xuan)
    CheckBox xuan;
    @BindView(R.id.sum)
    TextView sum;
    @BindView(R.id.toJiSuan)
    Button toJiSuan;
    private List<ShoppingBean.DataBean> list;
    private MyExpanAdapter myExpanAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gou_wu_che);
        ButterKnife.bind(this);
        IPresneterImpl iPresneter = new IPresneterImpl();
        iPresneter.ShoppIpesenter(new IModelImpl(iPresneter), this);
        xuan.setOnClickListener(this);
    }

    @Override
    public void showShopping(ShoppingBean shoppingBean) {
        this.list=shoppingBean.getData();
        Log.d(TAG, "showShopping: " + shoppingBean.getData().size());
        myExpanAdapter = new MyExpanAdapter(GouWuCheActivity.this, shoppingBean.getData(),this);
        expListview.setAdapter(myExpanAdapter);
        int groupCount = expListview.getCount();

        for (int i = 0; i < groupCount; i++) {

            expListview.expandGroup(i);

        }

    }

    @Override
    public void ShowCount(String count) {
        sum.setText("总价是:" + count);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.xuan:
                boolean checked = xuan.isChecked();
                //只需要改变集合里面的数据,就可以了,然后刷新
                for (int i = 0; i < list.size(); i++) {
                    ShoppingBean.DataBean dataBean = list.get(i);
                    dataBean.setParentIsSelected(checked);
                    //获得子集合,遍历
                    List<ShoppingBean.ChildBean> list3= dataBean.getList();

                    for (int j = 0; j < list3.size(); j++) {
                        list3.get(j).setChildIsSelected(checked);
                    }

                }
                //刷新即可
                myExpanAdapter.notifyDataSetChanged();
                //计算
                String count = SumUtils.sum(list);
                sum.setText("" + count);
                break;
        }
    }



}
适配器
public class MyExpanAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<ShoppingBean.DataBean> list;
    private IGouWuCheIView iView;

    public MyExpanAdapter(Context context, List<ShoppingBean.DataBean> list, IGouWuCheIView iView) {
        this.context = context;
        this.list = list;
        this.iView = iView;
    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getList().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) {
        ParentViewHolder parentViewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.layout_parent ,null);
            CheckBox parent_cb = convertView.findViewById(R.id.parent_box);
            TextView parent_title = convertView.findViewById(R.id.parent_title);
            parentViewHolder = new ParentViewHolder(parent_cb, parent_title);
            convertView.setTag(parentViewHolder);
        } else {
            parentViewHolder = (ParentViewHolder) convertView.getTag();
        }

        parentViewHolder.getParent_title().setText(list.get(groupPosition).getSellerName());
        parentViewHolder.getParent_cb().setChecked(list.get(groupPosition).isParentIsSelected());
        parentViewHolder.getParent_cb().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean selected = list.get(groupPosition).isParentIsSelected();
                selected = !selected;
                list.get(groupPosition).setParentIsSelected(selected);
                //点击父条目的复选框,子条目,相应的改变
//                将复选框的状态赋值给子复选框
                List<ShoppingBean.ChildBean> list1 = MyExpanAdapter.this.list.get(groupPosition).getList();
                for (int i = 0; i < list1.size(); i++) {
                    ShoppingBean.ChildBean childBean = list1.get(i);
                    childBean.setChildIsSelected(selected);
                }
                //刷新

                notifyDataSetChanged();
                //计算
                String sum = SumUtils.sum(list);
                iView.ShowCount(sum);
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        MyChildViewHolder myChildViewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.layout_child, null);
            CheckBox childCb = convertView.findViewById(R.id.child_box);
            TextView childTitle = convertView.findViewById(R.id.child_title);
            ImageView childIcon = convertView.findViewById(R.id.child_img);
            Button delete = convertView.findViewById(R.id.btn_commodity_delete);
            TextView childPrice = convertView.findViewById(R.id.tv_commodity_price);
            AddSubView addSubView = convertView.findViewById(R.id.addsub);
            myChildViewHolder = new MyChildViewHolder(childCb, childTitle, childPrice, addSubView, childIcon,delete);
            convertView.setTag(myChildViewHolder);
        } else {
            myChildViewHolder = (MyChildViewHolder) convertView.getTag();
        }
        //赋值
        myChildViewHolder.getChildCb().setChecked(list.get(groupPosition).getList().get(childPosition).isChildIsSelected());
        myChildViewHolder.getChildTitle().setText(list.get(groupPosition).getList().get(childPosition).getTitle());
        myChildViewHolder.getChildPrice().setText(list.get(groupPosition).getList().get(childPosition).getPrice() + "");
        myChildViewHolder.getAddSubView().setCount(list.get(groupPosition).getList().get(childPosition).getNum());
        myChildViewHolder.getAddSubView().setOnAddClickListener(new AddSubView.AddClickListener() {
            @Override
            public void onAddClick(View view, int count) {
                list.get(groupPosition).getList().get(childPosition).setNum(count);

                boolean selected = list.get(groupPosition).getList().get(childPosition).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(groupPosition).getList().get(childPosition).setNum(count);
                boolean selected = list.get(groupPosition).getList().get(childPosition).isChildIsSelected();
                //Log.d(TAG, "onAddClick() returned: " + "计算开始");
                if (selected){
                    String sum = SumUtils.sum(list);
                    //Log.d(TAG, "onAddClick() returned: " + sum);
                    iView.ShowCount(sum);
                }
            }
        });
        String images = list.get(groupPosition).getList().get(childPosition).getImages();
        String icon_url = images.split("\\|")[0];
        Uri uri=Uri.parse(icon_url);
        myChildViewHolder.getChildIcon().setImageURI(uri);
        //Glide.with(context).load(icon_url).into(myChildViewHolder.getChildIcon());
        myChildViewHolder.getChildCb().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean selected = list.get(groupPosition).getList().get(childPosition).isChildIsSelected();
                selected = !selected;
                list.get(groupPosition).getList().get(childPosition).setChildIsSelected(selected);

                List<ShoppingBean.ChildBean> list2 = MyExpanAdapter.this.list.get(groupPosition).getList();
                boolean flag = true;
                for (int j = 0; j < list2.size(); j++) {
                    if (!list2.get(j).isChildIsSelected()) {
                        flag = false;
                    }
                }
                list.get(groupPosition).setParentIsSelected(flag);
                //刷新
                notifyDataSetChanged();
                //计算
                String sum = SumUtils.sum(list);
                iView.ShowCount(sum);
            }
        });

        //删除
        myChildViewHolder.getDelete().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.get(groupPosition).getList().remove(childPosition);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
    //父分组的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;
        private Button delete;

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

        public Button getDelete() {
            return delete;
        }

        public void setDelete(Button delete) {
            this.delete = delete;
        }

        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;
        }
    }
}
一级列表的布局文件
<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_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/parent_title"
            />
</LinearLayout>
二级列表的布局文件
<LinearLayout
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/child_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:scaleX="0.6"
        android:scaleY="0.6" />
    <ImageView
        android:id="@+id/child_img"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:src="@mipmap/ic_launcher"
        android:layout_toRightOf="@id/child_box" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="15dp"
        android:layout_toRightOf="@id/child_img"
        android:orientation="vertical">
        <TextView
            android:id="@+id/child_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="酒红色纯红色纯羊毛西服套装"
            android:textColor="@android:color/black"
            android:textSize="12sp"
            android:textStyle="bold" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tv_commodity_attr"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="3dp"
                android:text="属性:粉蓝色"
                android:textSize="12sp"
                android:textColor="@color/colorPrimary" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tv_commodity_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="¥390"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="12sp"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/tv_commodity_num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="x1"
                android:textColor="@android:color/darker_gray" />
           <com.example.administrator.yuekao.view.AddSubView
               android:id="@+id/addsub"
               android:layout_marginLeft="6dp"
               android:layout_width="130dp"
               android:layout_height="30dp"/>
        </LinearLayout>


    </LinearLayout>


    <Button
        android:id="@+id/btn_commodity_delete"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="2"
        android:layout_gravity="center_vertical"
        android:text="删除"
        android:background="#ff44"
        />

</LinearLayout>
AddSub自定义View
public class AddSubView extends LinearLayout implements View.OnClickListener {

    private TextView sub;
    private TextView add;
    private EditText count;
    private AddClickListener addClickListener;
    private SubClickListener subClickListener;
    private static final String TAG = "AddSubView------------";
    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_layout, this);
        //获取控件
        sub = view.findViewById(R.id.sub);
        add = view.findViewById(R.id.add);
        count = view.findViewById(R.id.count);
        sub.setOnClickListener(this);
        add.setOnClickListener(this);


    }

    //点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //减少
            case R.id.sub:
                String c = count.getText().toString();
                int i = Integer.parseInt(c);
                if (i <= 0) {
                    return;
                }
                count.setText(--i + "");
                subClickListener.onSubClick(v,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(v,i1);
                Log.d(TAG, "onClick() returned: " + "加===");
                break;
        }
    }


    public int getCount() {
        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;
    }

}
AddSub自定义View布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/rl_edit"
        android:layout_width="120dp"
        android:background="@android:color/holo_orange_light"
        android:layout_height="30dp"

        >
        <TextView
            android:id="@+id/sub"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@android:color/black"
            android:background="@android:color/white"
            android:layout_margin="1dp"
            android:layout_height="match_parent"
            android:text=" - " />

        <EditText
            android:id="@+id/count"
            android:background="@null"
            android:layout_width="50dp"
            android:gravity="center"
            android:layout_height="30dp" />

        <TextView
            android:id="@+id/add"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_margin="1dp"
            android:background="@android:color/white"
            android:layout_height="match_parent"
            android:text=" + " />
    </LinearLayout>
</LinearLayout>
计算的工具类
public class SumUtils {
    public static String sum(List<ShoppingBean.DataBean> list) {
        double sum = 0;
        for (int i = 0; i < list.size(); i++) {
            List<ShoppingBean.ChildBean> list1 = list.get(i).getList();
            for (int j = 0; j < list1.size(); j++) {
                ShoppingBean.ChildBean childBean = list1.get(j);
                if (childBean.isChildIsSelected()) {
                    sum += childBean.getPrice()*childBean.getNum();
                }
            }
        }
        return Double.toString(sum);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值