ExpandableListView购物车模版

二级列表购物车,简单实用,接口自己看着写一下.

MainActivity:

package com.bwie.dongchangqi.shopcar;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
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.bwie.dongchangqi.shopcar.Adapters.ExpandbAdpt;
import com.bwie.dongchangqi.shopcar.Beans.IsnoBean;
import com.bwie.dongchangqi.shopcar.Beans.MyBean;
import com.bwie.dongchangqi.shopcar.Callbacks.ChiledCheckBoxCallBack;
import com.bwie.dongchangqi.shopcar.Callbacks.ChiledDownCallBack;
import com.bwie.dongchangqi.shopcar.Callbacks.ChiledUpCallBack;
import com.bwie.dongchangqi.shopcar.Callbacks.GroupCallBack;
import com.bwie.dongchangqi.shopcar.Presenters.Presenter;
import com.bwie.dongchangqi.shopcar.Views.VCallback;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity implements VCallback,GroupCallBack,ChiledCheckBoxCallBack,ChiledUpCallBack,ChiledDownCallBack {

    @BindView(R.id.expandablelistview_show)
    ExpandableListView Epdblv;
    @BindView(R.id.cb_all)
    CheckBox cbAll;
    @BindView(R.id.tv_all_price)
    TextView tvAllPrice;
    @BindView(R.id.tv_all_geshu)
    TextView tvAllGeshu;
    @BindView(R.id.btn_delete)
    Button btnDelete;

    List<MyBean.DataBean> data;
    List<IsnoBean> GroupIsno;   //Group集合
    List<List<IsnoBean>> ChiledIsno;
    ExpandbAdpt expandbAdpt;
    int allPrice = 0;
    int allGeshu = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        Presenter presenter = new Presenter();
        presenter.attachView(this);
        presenter.getPData("product/getCarts?source=android&uid=3063","http://120.27.23.105/");

    }

    @Override
    public void onSuccess(Object object) {
        Log.e("MainActivity","请求网络成功");
        MyBean myBean = (MyBean) object;
        data = myBean.getData();

        if(data!=null||data.size()!=0){
            GroupIsno = new ArrayList<IsnoBean>();
            ChiledIsno = new ArrayList<List<IsnoBean>>();
            for(int i=0;i<data.size();i++){
                GroupIsno.add(new IsnoBean(false));
                List<IsnoBean> li = new ArrayList<IsnoBean>();
                for(int j=0;j<data.get(i).getList().size();j++){
                    li.add(new IsnoBean(false));
                }
                ChiledIsno.add(li);
            }

            expandbAdpt = new ExpandbAdpt(this,data,GroupIsno,ChiledIsno);
            expandbAdpt.setChiledcheckboxcallback(this);
            expandbAdpt.setChiledDownCallBack(this);
            expandbAdpt.setChiledUpCallBack(this);
            expandbAdpt.setGroupCheckBox(this);
            Epdblv.setAdapter(expandbAdpt);
            int groupCount = expandbAdpt.getGroupCount();
            for (int i=0; i<groupCount; i++) {
                Epdblv.expandGroup(i);
            }
            Epdblv.setGroupIndicator(null);

        }

    }

    @Override
    public void onFalsed(Throwable throwable) {
        Log.e("MainActivity","请求网络失败");
    }

    @OnClick({R.id.cb_all, R.id.btn_delete})
    public void onClick(View view) {
        AlertDialog alert;
        switch (view.getId()) {
            case R.id.cb_all:
                boolean checked = cbAll.isChecked();
                for (int i = 0; i < GroupIsno.size(); i++) {
                    GroupIsno.get(i).setIsno(checked);
                    List<IsnoBean> isnoBeen = ChiledIsno.get(i);
                    for (int j = 0; j < isnoBeen.size(); j++) {
                        isnoBeen.get(j).setIsno(checked);
                    }
                }
                expandbAdpt.notifyDataSetChanged();
                ZongjiaAndGeshu();
                break;
            case R.id.btn_delete:
                int q = 0;
                for(int i=0;i<ChiledIsno.size();i++){
                    List<IsnoBean> isnoBeen = ChiledIsno.get(i);
                    for(int j=0;j<isnoBeen.size();j++){
                        if(isnoBeen.get(j).isno()){
                            q++;
                        }
                    }
                }
                if (q == 0) {
                    Toast.makeText(this, "请选择要移除的商品", Toast.LENGTH_LONG).show();
                    return;
                }
                alert = new AlertDialog.Builder(this).create();
                //alert.setTitle("操作提示");
                alert.setMessage("确认要删除这"+q+"种商品吗");
                alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                return;
                            }
                        });
                alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                doDelete();
                                if(data==null||data.size()==0){
                                    cbAll.setChecked(false);
                                }
                            }
                        });
                alert.show();
                ZongjiaAndGeshu();

                break;
        }
    }

    @Override
    public void onGroupCheckBox(boolean checked, int i) {
        GroupIsno.get(i).setIsno(checked);
        List<IsnoBean> isnoBeen = ChiledIsno.get(i);
        for(int u=0;u<isnoBeen.size();u++){
            isnoBeen.get(u).setIsno(checked);
        }
        expandbAdpt.notifyDataSetChanged();

        AllCheckBox();

        ZongjiaAndGeshu();
    }

    @Override
    public void onChiledUp(int num, int i, int i1) {
        data.get(i).getList().get(i1).setNum(num);
        expandbAdpt.notifyDataSetChanged();
        AllCheckBox();
        ZongjiaAndGeshu();
    }

    @Override
    public void onChiledDown(int num, int i, int i1) {
        data.get(i).getList().get(i1).setNum(num);
        GaiGroupChexkBox(i);
        expandbAdpt.notifyDataSetChanged();
        ZongjiaAndGeshu();

    }

    @Override
    public void onChiledCheckBox(boolean isno, int i, int i1) {
        ChiledIsno.get(i).get(i1).setIsno(isno);

        GaiGroupChexkBox(i);

        expandbAdpt.notifyDataSetChanged();

        AllCheckBox();

        ZongjiaAndGeshu();
    }

    //根据商品的复选框改变店铺的复选框状态
    private void GaiGroupChexkBox(int i) {
        int t = 0;
        for(int p=0;p<ChiledIsno.get(i).size();p++){
            if(!ChiledIsno.get(i).get(p).isno()){
                t = 1;
            }
        }
        if(t==0){
            GroupIsno.get(i).setIsno(true);
        }else{
            GroupIsno.get(i).setIsno(false);
        }
    }

    //算总价格和总个数---每次点击事件完之后都调用此方法
    public void ZongjiaAndGeshu(){
        allPrice = 0;
        allGeshu = 0;
        for(int i=0;i<ChiledIsno.size();i++){
            List<IsnoBean> isnoBeen = ChiledIsno.get(i);
            for(int j=0;j<isnoBeen.size();j++){
                MyBean.DataBean.ListBean listBean = data.get(i).getList().get(j);
                if(isnoBeen.get(j).isno()){
                    allPrice+=(int)(listBean.getNum()*listBean.getBargainPrice());
                    allGeshu+=listBean.getNum();
                }
            }
        }
        tvAllPrice.setText("合计:"+allPrice);
        tvAllGeshu.setText("个数:"+allGeshu);
    }
    //根据所有店铺的状态,改变总的复选框状态
    private void AllCheckBox() {
        int e = 0;
        for(int r=0;r<GroupIsno.size();r++){
            if(!GroupIsno.get(r).isno()){
                e = 1;
            }
        }
        if(e==0){
            cbAll.setChecked(true);
        }else{
            cbAll.setChecked(false);
        }
    }

    //删除
    public void doDelete(){

        List<IsnoBean> toDeleteGroup = new ArrayList<IsnoBean>();
        List<List<IsnoBean>> toDeleteChiledGr = new ArrayList<List<IsnoBean>>();
        List<MyBean.DataBean> toDeleteGroupData = new ArrayList<MyBean.DataBean>();
        for(int i=0;i<GroupIsno.size();i++){
            IsnoBean deleteGroup = GroupIsno.get(i);
            List<IsnoBean> isnoBeen = ChiledIsno.get(i);
            MyBean.DataBean dataBean = data.get(i);
            if(deleteGroup.isno()){
                toDeleteGroup.add(deleteGroup);
                toDeleteChiledGr.add(isnoBeen);
                toDeleteGroupData.add(dataBean);
            }
            List<IsnoBean> toDeleteChiled = new ArrayList<IsnoBean>();
            List<MyBean.DataBean.ListBean> toDeleteChiledData = new ArrayList<MyBean.DataBean.ListBean>();
            for(int e=0;e<isnoBeen.size();e++){
                MyBean.DataBean.ListBean listBean = dataBean.getList().get(e);
                IsnoBean isnoBeanchiled = isnoBeen.get(e);
                if(isnoBeanchiled.isno()){
                    toDeleteChiled.add(isnoBeanchiled);
                    toDeleteChiledData.add(listBean);
                }
            }
            isnoBeen.removeAll(toDeleteChiled);
            dataBean.getList().removeAll(toDeleteChiledData);
        }

        GroupIsno.removeAll(toDeleteGroup);
        ChiledIsno.removeAll(toDeleteChiledGr);
        data.removeAll(toDeleteGroupData);
        expandbAdpt.notifyDataSetChanged();
    }
}


Adapter:

package com.bwie.dongchangqi.shopcar.Adapters;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.bwie.dongchangqi.shopcar.Beans.IsnoBean;
import com.bwie.dongchangqi.shopcar.Beans.MyBean;
import com.bwie.dongchangqi.shopcar.Callbacks.ChiledCheckBoxCallBack;
import com.bwie.dongchangqi.shopcar.Callbacks.ChiledDownCallBack;
import com.bwie.dongchangqi.shopcar.Callbacks.ChiledUpCallBack;
import com.bwie.dongchangqi.shopcar.Callbacks.GroupCallBack;
import com.bwie.dongchangqi.shopcar.R;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * Created by Administrator on 2017/12/16 0016.
 */

public class ExpandbAdpt extends BaseExpandableListAdapter{
    Context context;
    List<MyBean.DataBean> data;
    List<IsnoBean> GroupIsno;   //Group集合
    List<List<IsnoBean>> ChiledIsno;   //Chiled集合

    public ExpandbAdpt(Context context,List<MyBean.DataBean> data, List<IsnoBean> groupIsno, List<List<IsnoBean>> chiledIsno) {
        this.context = context;
        this.data = data;
        this.GroupIsno = groupIsno;
        this.ChiledIsno = chiledIsno;
    }

    private GroupCallBack groupcallback;
    public void setGroupCheckBox(GroupCallBack groupcallback){
        this.groupcallback = groupcallback;
    }
    private ChiledCheckBoxCallBack chiledcheckboxcallback;
    public void setChiledcheckboxcallback(ChiledCheckBoxCallBack chiledcheckboxcallback){
        this.chiledcheckboxcallback = chiledcheckboxcallback;
    }
    private ChiledDownCallBack chileddowncallback;
    public void setChiledDownCallBack(ChiledDownCallBack chileddowncallback){
        this.chileddowncallback = chileddowncallback;
    }
    private ChiledUpCallBack chiledupcallback;
    public void setChiledUpCallBack(ChiledUpCallBack chiledupcallback){
        this.chiledupcallback = chiledupcallback;
    }

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

    @Override
    public int getChildrenCount(int i) {
        return data.get(i).getList().size();
    }

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

    @Override
    public Object getChild(int i, int i1) {
        return data.get(i).getList().get(i1);
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

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

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        MyBean.DataBean dataBean = data.get(i);
        IsnoBean isnoBean = GroupIsno.get(i);
        final ViewHodler1 vh1;
        if(view==null){
            vh1 = new ViewHodler1();
            view = View.inflate(context, R.layout.expanbadlelistview_store_item,null);
            vh1.cbStore = view.findViewById(R.id.store_cb);
            vh1.tvStoreName = view.findViewById(R.id.store_name_show);
            view.setTag(vh1);
        }else{
            vh1 = (ViewHodler1) view.getTag();
        }
        vh1.tvStoreName.setText(dataBean.getSellerName());
        vh1.cbStore.setChecked(isnoBean.isno());
        vh1.cbStore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked = vh1.cbStore.isChecked();
                groupcallback.onGroupCheckBox(checked,i);
            }
        });
        return view;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        final MyBean.DataBean.ListBean listBean = data.get(i).getList().get(i1);
        IsnoBean isnoBean = ChiledIsno.get(i).get(i1);
        final ViewHolder2 vh2;
        if(view==null){
            vh2 = new ViewHolder2();
            view = View.inflate(context, R.layout.expanbadlelistview_commodite_item,null);
            vh2.cbCommodity = view.findViewById(R.id.cb_commodity_show);
            vh2.imgCommodityPicture = view.findViewById(R.id.commodity_img);
            vh2.tvCommodityTitle = view.findViewById(R.id.commodity_title);
            vh2.tvCommodityPrice = view.findViewById(R.id.commodity_price);
            vh2.btnCommodityUp = view.findViewById(R.id.commodity_geshu_up);
            vh2.btnCommodityShow = view.findViewById(R.id.commodity_geshu_show);
            vh2.btnCommodityDown = view.findViewById(R.id.commodity_geshu_down);
            view.setTag(vh2);
        }else{
            vh2 = (ViewHolder2) view.getTag();
        }
        String[] split = listBean.getImages().split("\\|");
        Uri parse = Uri.parse(split[0]);
        vh2.imgCommodityPicture.setImageURI(parse);
        vh2.tvCommodityTitle.setText(listBean.getTitle());
        vh2.tvCommodityPrice.setText("价格:"+listBean.getBargainPrice());
        vh2.cbCommodity.setChecked(isnoBean.isno());
        vh2.btnCommodityShow.setText(listBean.getNum()+"");
        vh2.cbCommodity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked = vh2.cbCommodity.isChecked();
                chiledcheckboxcallback.onChiledCheckBox(checked,i,i1);
            }
        });
        vh2.btnCommodityDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = vh2.btnCommodityShow.getText().toString();
                int num = Integer.valueOf(s).intValue();
                if(num>1){
                    num--;
                }
                chileddowncallback.onChiledDown(num,i,i1);
            }
        });
        vh2.btnCommodityUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = vh2.btnCommodityShow.getText().toString();
                int num = Integer.valueOf(s).intValue();
                num++;
                chiledupcallback.onChiledUp(num,i,i1);
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
    class ViewHodler1{
        private CheckBox cbStore;
        private TextView tvStoreName;
    }
    class ViewHolder2{
        private CheckBox cbCommodity;
        private TextView tvCommodityTitle,tvCommodityPrice;
        private SimpleDraweeView imgCommodityPicture;
        private Button btnCommodityUp,btnCommodityShow,btnCommodityDown;
    }
}


主布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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="com.bwie.dongchangqi.shopcar.MainActivity">

    <ExpandableListView
        android:id="@+id/expandablelistview_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scrollbars="none"
        android:divider="@null"
        >
    </ExpandableListView>

    <LinearLayout
        android:orientation="horizontal"
        android:background="#fff"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <CheckBox
            android:id="@+id/cb_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tv_all_price"
            android:text="合计:0"
            android:layout_width="100dp"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tv_all_geshu"
            android:text="个数:0"
            android:layout_width="100dp"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/btn_delete"
            android:text="删除"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</RelativeLayout>

Group布局:xml名字:expanbadlelistview_store_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <CheckBox
        android:id="@+id/store_cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/store_name_show"
        android:text="storename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Chiled布局:xml名字:expanbadlelistview_commodite_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <CheckBox
        android:id="@+id/cb_commodity_show"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/commodity_img"
        android:layout_width="100dp"
        android:layout_height="100dp"
        fresco:placeholderImage="@mipmap/ic_launcher"
        />

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

        <TextView
            android:id="@+id/commodity_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="title" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/commodity_price"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:text="price"
                android:textColor="#f00" />
            <Button
                android:id="@+id/commodity_geshu_down"
                android:background="@drawable/btn"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:text="-" />
            <Button
                android:id="@+id/commodity_geshu_show"
                android:background="@drawable/btn"
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:text="1" />
            <Button
                android:id="@+id/commodity_geshu_up"
                android:background="@drawable/btn"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:text="+" />
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值